# Sorting strings that contain numbers

**URL:** <https://discuss.elastic.co/t/sorting-strings-that-contain-numbers/9487>\
**Category:** Elasticsearch\
**Created:** [October 28, 2012, 4:48pm UTC](https://discuss.elastic.co/t/sorting-strings-that-contain-numbers/9487 "2012-10-28T16:48:12Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nick\_Hoffman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nick_hoffman/32/1872_2.png) [@Nick\_Hoffman](https://discuss.elastic.co/u/Nick_Hoffman)\
**Post date:** [October 28, 2012, 4:48pm UTC](https://discuss.elastic.co/t/sorting-strings-that-contain-numbers/9487/1 "2012-10-28T16:48:12Z")

</div>

Hi guys. When sorting on a field that's a string, strings that contain  
numbers aren't sorted properly.

For example, with these documents:  
{ name: "Bob: 3 points" }  
{ name: "Bob: 10 points" }  
{ name: "Bob: 2 points" }

When ES sorts on the "name" field, the documents are returned in this order:  
{ name: "Bob: 10 points" }  
{ name: "Bob: 2 points" }  
{ name: "Bob: 3 points" }

How can we get ES to return the documents in the following order?  
{ name: "Bob: 2 points" }  
{ name: "Bob: 3 points" }  
{ name: "Bob: 10 points" }

Thanks,  
Nick

--

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [October 28, 2012, 7:01pm UTC](https://discuss.elastic.co/t/sorting-strings-that-contain-numbers/9487/2 "2012-10-28T19:01:20Z")

</div>

IMHO you should index docs with 02 instead of 2.

--  
David 😉  
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

Le 28 oct. 2012 à 17:48, Nick Hoffman [nick@deadorange.com](mailto:nick@deadorange.com) a écrit :

Hi guys. When sorting on a field that's a string, strings that contain numbers aren't sorted properly.

For example, with these documents:  
{ name: "Bob: 3 points" }  
{ name: "Bob: 10 points" }  
{ name: "Bob: 2 points" }

When ES sorts on the "name" field, the documents are returned in this order:  
{ name: "Bob: 10 points" }  
{ name: "Bob: 2 points" }  
{ name: "Bob: 3 points" }

How can we get ES to return the documents in the following order?  
{ name: "Bob: 2 points" }  
{ name: "Bob: 3 points" }  
{ name: "Bob: 10 points" }

## Thanks, Nick

--

---

<div class="post-metadata">

**Author:** ![Nick\_Hoffman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nick_hoffman/32/1872_2.png) [@Nick\_Hoffman](https://discuss.elastic.co/u/Nick_Hoffman)\
**Post date:** [October 28, 2012, 7:10pm UTC](https://discuss.elastic.co/t/sorting-strings-that-contain-numbers/9487/3 "2012-10-28T19:10:52Z")

</div>

Hi David. Prefixing numbers with zeros won't work because that assumes that  
there's a constant number of digits in the number.

On Sunday, 28 October 2012 15:01:37 UTC-4, David Pilato wrote:

> IMHO you should index docs with 02 instead of 2.
> 
> --  
> David 😉  
> Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> 
> Le 28 oct. 2012 à 17:48, Nick Hoffman \<[ni...@deadorange.com](mailto:ni...@deadorange.com) \<javascript:\>\>  
> a écrit :
> 
> Hi guys. When sorting on a field that's a string, strings that contain  
> numbers aren't sorted properly.
> 
> For example, with these documents:  
> { name: "Bob: 3 points" }  
> { name: "Bob: 10 points" }  
> { name: "Bob: 2 points" }
> 
> When ES sorts on the "name" field, the documents are returned in this  
> order:  
> { name: "Bob: 10 points" }  
> { name: "Bob: 2 points" }  
> { name: "Bob: 3 points" }
> 
> How can we get ES to return the documents in the following order?  
> { name: "Bob: 2 points" }  
> { name: "Bob: 3 points" }  
> { name: "Bob: 10 points" }
> 
> Thanks,  
> Nick
> 
> --

--

---

<div class="post-metadata">

**Author:** ![Axsuul](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/axsuul/32/2663_2.png) [@Axsuul](https://discuss.elastic.co/u/Axsuul)\
**Post date:** [October 28, 2012, 7:34pm UTC](https://discuss.elastic.co/t/sorting-strings-that-contain-numbers/9487/4 "2012-10-28T19:34:42Z")

</div>

Related  
[http://elasticsearch-users.115913.n3.nabble.com/Sorting-a-string-field-numerically-td4024557.html#a4024561](http://elasticsearch-users.115913.n3.nabble.com/Sorting-a-string-field-numerically-td4024557.html#a4024561)

On Sunday, October 28, 2012 12:10:52 PM UTC-7, Nick Hoffman wrote:

> Hi David. Prefixing numbers with zeros won't work because that assumes  
> that there's a constant number of digits in the number.
> 
> On Sunday, 28 October 2012 15:01:37 UTC-4, David Pilato wrote:
> 
> > IMHO you should index docs with 02 instead of 2.
> > 
> > --  
> > David 😉  
> > Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> > 
> > Le 28 oct. 2012 à 17:48, Nick Hoffman [ni...@deadorange.com](mailto:ni...@deadorange.com) a écrit :
> > 
> > Hi guys. When sorting on a field that's a string, strings that contain  
> > numbers aren't sorted properly.
> > 
> > For example, with these documents:  
> > { name: "Bob: 3 points" }  
> > { name: "Bob: 10 points" }  
> > { name: "Bob: 2 points" }
> > 
> > When ES sorts on the "name" field, the documents are returned in this  
> > order:  
> > { name: "Bob: 10 points" }  
> > { name: "Bob: 2 points" }  
> > { name: "Bob: 3 points" }
> > 
> > How can we get ES to return the documents in the following order?  
> > { name: "Bob: 2 points" }  
> > { name: "Bob: 3 points" }  
> > { name: "Bob: 10 points" }
> > 
> > Thanks,  
> > Nick
> > 
> > --

--

---

<div class="post-metadata">

**Author:** ![Chris\_Male](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chris_male/32/2607_2.png) [@Chris\_Male](https://discuss.elastic.co/u/Chris_Male)\
**Post date:** [October 29, 2012, 2:40am UTC](https://discuss.elastic.co/t/sorting-strings-that-contain-numbers/9487/5 "2012-10-29T02:40:02Z")

</div>

Could you split the data into multiple fields? So have a name field "Bob,  
Anne, etc" which is a string and a points field "3, 10, 2" which is a  
number. Then sort both fields together, name coming first?

On Monday, October 29, 2012 5:48:12 AM UTC+13, Nick Hoffman wrote:

> Hi guys. When sorting on a field that's a string, strings that contain  
> numbers aren't sorted properly.
> 
> For example, with these documents:  
> { name: "Bob: 3 points" }  
> { name: "Bob: 10 points" }  
> { name: "Bob: 2 points" }
> 
> When ES sorts on the "name" field, the documents are returned in this  
> order:  
> { name: "Bob: 10 points" }  
> { name: "Bob: 2 points" }  
> { name: "Bob: 3 points" }
> 
> How can we get ES to return the documents in the following order?  
> { name: "Bob: 2 points" }  
> { name: "Bob: 3 points" }  
> { name: "Bob: 10 points" }
> 
> Thanks,  
> Nick

--

---

<div class="post-metadata">

**Author:** ![Nick\_Hoffman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nick_hoffman/32/1872_2.png) [@Nick\_Hoffman](https://discuss.elastic.co/u/Nick_Hoffman)\
**Post date:** [October 29, 2012, 2:54am UTC](https://discuss.elastic.co/t/sorting-strings-that-contain-numbers/9487/6 "2012-10-29T02:54:01Z")

</div>

I'd definitely do that if I could, Chris. The strings that I'm indexing are  
names of objects that can't be split, unfortunately. E.g.

Megatron (UN-04)  
The Amazing Spider-Man #44  
G2 Optimus Prime

This is why the sorting has to happen within ES.

On Sunday, 28 October 2012 22:40:02 UTC-4, Chris Male wrote:

> Could you split the data into multiple fields? So have a name field "Bob,  
> Anne, etc" which is a string and a points field "3, 10, 2" which is a  
> number. Then sort both fields together, name coming first?

--

---

<div class="post-metadata">

**Author:** ![jprante](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jprante/32/44941_2.png) [@jprante](https://discuss.elastic.co/u/jprante)\
**Post date:** [October 30, 2012, 2:34pm UTC](https://discuss.elastic.co/t/sorting-strings-that-contain-numbers/9487/7 "2012-10-30T14:34:31Z")

</div>

Hi Nick,

thanks for your inspiration. It's a great idea. I just hacked together a  
plugin that can perform the desired sort by using a natural sort key in a  
Lucene token filter.

> **[GitHub - jprante/elasticsearch-analysis-naturalsort: Natural sort plugin for...](https://github.com/jprante/elasticsearch-analysis-naturalsort)**
>
> Natural sort plugin for Elasticsearch. Contribute to jprante/elasticsearch-analysis-naturalsort development by creating an account on GitHub.

The README is a little short but I hope it helps.

See also the test  
file [https://github.com/jprante/elasticsearch-analysis-naturalsort/blob/master/src/test/java/org/elasticsearch/index/analysis/naturalsort/NaturalSortKeyTests.java](https://github.com/jprante/elasticsearch-analysis-naturalsort/blob/master/src/test/java/org/elasticsearch/index/analysis/naturalsort/NaturalSortKeyTests.java)

As a bonus, a collator key sort is included (since the natural sort key  
extends the collator key, you have to add a "locale" parameter to the token  
filter if you want locale-sensitive sort)

Cheers,

Jörg

On Monday, October 29, 2012 3:54:01 AM UTC+1, Nick Hoffman wrote:

> I'd definitely do that if I could, Chris. The strings that I'm indexing  
> are names of objects that can't be split, unfortunately. E.g.
> 
> Megatron (UN-04)  
> The Amazing Spider-Man #44  
> G2 Optimus Prime
> 
> This is why the sorting has to happen within ES.
> 
> On Sunday, 28 October 2012 22:40:02 UTC-4, Chris Male wrote:
> 
> > Could you split the data into multiple fields? So have a name field "Bob,  
> > Anne, etc" which is a string and a points field "3, 10, 2" which is a  
> > number. Then sort both fields together, name coming first?

--

---

<div class="post-metadata">

**Author:** ![bbehling](https://avatars.discourse-cdn.com/v4/letter/b/f1d935/32.png) [@bbehling](https://discuss.elastic.co/u/bbehling)\
**Post date:** [January 23, 2015, 6:13pm UTC](https://discuss.elastic.co/t/sorting-strings-that-contain-numbers/9487/8 "2015-01-23T18:13:55Z")

</div>

Jörg,

Will your plugin work with ES version 1.3.4?

---

<div class="post-metadata">

**Author:** ![jprante](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jprante/32/44941_2.png) [@jprante](https://discuss.elastic.co/u/jprante)\
**Post date:** [January 26, 2015, 12:45pm UTC](https://discuss.elastic.co/t/sorting-strings-that-contain-numbers/9487/9 "2015-01-26T12:45:06Z")

</div>

Yes, there is a version for ES 1.3.4

Jörg

On Fri, Jan 23, 2015 at 7:13 PM, bbehling [brian.behling@gmail.com](mailto:brian.behling@gmail.com) wrote:

> Jörg,
> 
> Will your plugin work with ES version 1.3.4?
> 
> --  
> View this message in context:  
> [http://elasticsearch-users.115913.n3.nabble.com/Sorting-strings-that-contain-numbers-tp4024553p4069501.html](http://elasticsearch-users.115913.n3.nabble.com/Sorting-strings-that-contain-numbers-tp4024553p4069501.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/1422036835206-4069501.post%40n3.nabble.com](https://groups.google.com/d/msgid/elasticsearch/1422036835206-4069501.post%40n3.nabble.com)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAKdsXoHgRyFRgTMnv61ukyWNfEw4hKX3UHHFdEg7n0upqKLJBQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAKdsXoHgRyFRgTMnv61ukyWNfEw4hKX3UHHFdEg7n0upqKLJBQ%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![iti\_netapp.com](https://avatars.discourse-cdn.com/v4/letter/i/9e8a1a/32.png) [@iti\_netapp.com](https://discuss.elastic.co/u/iti_netapp.com)\
**Post date:** [November 29, 2016, 11:03pm UTC](https://discuss.elastic.co/t/sorting-strings-that-contain-numbers/9487/10 "2016-11-29T23:03:37Z")

</div>

Hi,  
Are you planning to have a version for ES for ES 5.0?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 10:05pm UTC](https://discuss.elastic.co/t/sorting-strings-that-contain-numbers/9487/11 "2017-07-05T22:05:22Z")

</div>


