# Sort ascending with null values last

**URL:** <https://discuss.elastic.co/t/sort-ascending-with-null-values-last/5884>\
**Category:** Elasticsearch\
**Created:** [November 16, 2011, 4:46am UTC](https://discuss.elastic.co/t/sort-ascending-with-null-values-last/5884 "2011-11-16T04:46:04Z")\
**Posts on this page:** 5\
**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:** [November 16, 2011, 4:46am UTC](https://discuss.elastic.co/t/sort-ascending-with-null-values-last/5884/1 "2011-11-16T04:46:04Z")

</div>

Ahoy. When sorting on a string field in ascending order, is there a way to  
move the documents with a null value for the sort-field to the end of the  
results?

For example, with the setup below, we sort by ascending city, but Bob has  
no city and is the first result. Can we cause Bob to be the last result?

curl -s -X DELETE localhost:9200/test  
curl -X PUT 'localhost:9200/test?pretty=true'

curl -XPUT localhost:9200/test/people/1 -d '{  
"name" : "Jane",  
"city" : "Toronto"  
}'

curl -XPUT localhost:9200/test/people/2 -d '{  
"name": "Jack",  
"city": "Ottawa"  
}'

curl -XPUT localhost:9200/test/people/3 -d '{  
"name": "Bob"  
}'

curl 'localhost:9200/test/people/\_search' -d '{ sort: [{"city": {"order":  
"asc"} }] }'

---

<div class="post-metadata">

**Author:** ![Jan\_Fiedler](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jan_fiedler/32/2518_2.png) [@Jan\_Fiedler](https://discuss.elastic.co/u/Jan_Fiedler)\
**Post date:** [November 16, 2011, 11:49am UTC](https://discuss.elastic.co/t/sort-ascending-with-null-values-last/5884/2 "2011-11-16T11:49:34Z")

</div>

I created an issue for this some time ago (as I need the feature too):  
[https://github.com/elasticsearch/elasticsearch/issues/896](https://github.com/elasticsearch/elasticsearch/issues/896) (just in case you  
want to vote it up ... ;-))

---

<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:** [November 16, 2011, 7:54pm UTC](https://discuss.elastic.co/t/sort-ascending-with-null-values-last/5884/3 "2011-11-16T19:54:44Z")

</div>

Thanks for the link, Jan. I just commented on it.

---

<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:** [November 16, 2011, 7:56pm UTC](https://discuss.elastic.co/t/sort-ascending-with-null-values-last/5884/4 "2011-11-16T19:56:25Z")

</div>

The only workaround that I can think of is to modify one's query to exclude  
documents with a null value for the sort-field.

Can you think of any other workarounds until #896 is implemented?

---

<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 6, 2017, 3:48am UTC](https://discuss.elastic.co/t/sort-ascending-with-null-values-last/5884/5 "2017-07-06T03:48:24Z")

</div>


