# How to perform natural sort on a field in Elasticsearch?

**URL:** <https://discuss.elastic.co/t/how-to-perform-natural-sort-on-a-field-in-elasticsearch/285854>\
**Category:** Elasticsearch\
**Created:** [October 4, 2021, 8:57pm UTC](https://discuss.elastic.co/t/how-to-perform-natural-sort-on-a-field-in-elasticsearch/285854 "2021-10-04T20:57:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tranindigo](https://avatars.discourse-cdn.com/v4/letter/t/b5ac83/32.png) [@tranindigo](https://discuss.elastic.co/u/tranindigo)\
**Post date:** [October 4, 2021, 8:57pm UTC](https://discuss.elastic.co/t/how-to-perform-natural-sort-on-a-field-in-elasticsearch/285854/1 "2021-10-04T20:57:51Z")

</div>

I have an Elasticsearch `keyword` field called `LocationNumber` that I want to sort "alphanumerically" (or "naturally") so that it is easy for users to read. I have been researching this for days and couldn't find any real discussions or solutions.

What I want is to sort numbers and letters in a "natural" way.

This is the default way that Elasticsearch sorts in ascending order, and is **NOT** what I want:

```auto
0102, 1, 101, 101A, 101B, 2, B101, CC452PD452, Store102, Store102A

```

This is what I want when I sort in ascending order:

```auto
1, 2, 101, 0102, 101A, 101B, B101, CC452PD452, Store102, Store102A

```

I saw a natural sort plugin ([here](https://github.com/jprante/elasticsearch-analysis-naturalsort)), but I think it's too old, and it didn't install for the latest version of Elasticsearch.

Is there a way to implement a "natural" sort either during index time or query time? I prefer an index time solution because a query time solution will consume too much memory.

---

<div class="post-metadata">

**Author:** ![tranindigo](https://avatars.discourse-cdn.com/v4/letter/t/b5ac83/32.png) [@tranindigo](https://discuss.elastic.co/u/tranindigo)\
**Post date:** [October 13, 2021, 8:04pm UTC](https://discuss.elastic.co/t/how-to-perform-natural-sort-on-a-field-in-elasticsearch/285854/2 "2021-10-13T20:04:02Z")

</div>

I found an answer: [ICU Collation Keyword Field | Elasticsearch Plugins and Integrations [7.15] | Elastic](https://www.elastic.co/guide/en/elasticsearch/plugins/current/analysis-icu-collation-keyword-field.html)

You have to use the "analysis-icu" plugin and use the field type `icu_collation_keyword` as a multi-field with "numeric = true". You can then sort using this multi-field.

This relies on the Unicode Collation Algorithm.

---

<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:** [November 10, 2021, 8:04pm UTC](https://discuss.elastic.co/t/how-to-perform-natural-sort-on-a-field-in-elasticsearch/285854/3 "2021-11-10T20:04:53Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
