# How to sort number stored as string (keyword)

**URL:** <https://discuss.elastic.co/t/how-to-sort-number-stored-as-string-keyword/230551>\
**Category:** Elasticsearch\
**Created:** [April 30, 2020, 12:42pm UTC](https://discuss.elastic.co/t/how-to-sort-number-stored-as-string-keyword/230551 "2020-04-30T12:42:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![AffanShahab](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/affanshahab/32/67431_2.png) [@AffanShahab](https://discuss.elastic.co/u/AffanShahab)\
**Post date:** [April 30, 2020, 12:42pm UTC](https://discuss.elastic.co/t/how-to-sort-number-stored-as-string-keyword/230551/1 "2020-04-30T12:42:32Z")

</div>

Some numerical attribute in my data is mapped as keyword and stored as string. I want to sort it as 1, 2, 10, 15 or when descending. 15, 10, 2, 1. I have tried multiple solutions available online but nothing works. Data will contain only number as string. There won't be any mix like ABC10.  
I can not change mapping and performance is not a big issue

---

<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:** [April 30, 2020, 4:58pm UTC](https://discuss.elastic.co/t/how-to-sort-number-stored-as-string-keyword/230551/2 "2020-04-30T16:58:40Z")

</div>

Welcome!

You need to change the mapping (reindex that is). Or use [https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-sort-context.html](https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-sort-context.html)

---

<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:** [May 28, 2020, 4:58pm UTC](https://discuss.elastic.co/t/how-to-sort-number-stored-as-string-keyword/230551/3 "2020-05-28T16:58:41Z")

</div>

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