# Sort not working as expected

**URL:** <https://discuss.elastic.co/t/sort-not-working-as-expected/360371>\
**Category:** Elastic Search\
**Created:** [May 28, 2024, 11:22am UTC](https://discuss.elastic.co/t/sort-not-working-as-expected/360371 "2024-05-28T11:22:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gjahagir](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gjahagir/32/125175_2.png) [@gjahagir](https://discuss.elastic.co/u/gjahagir)\
**Post date:** [May 28, 2024, 11:22am UTC](https://discuss.elastic.co/t/sort-not-working-as-expected/360371/1 "2024-05-28T11:22:24Z")

</div>

Hi,  
I have an index with Text field along with the corresponding Keyword field.  
Index uses custom analyser with Keyword tokenizer using lowercase filter.

this is the index structure -

```auto
{
  "settings": {
    "analysis": {
      "analyzer": {
        "CustomTokenizer": {
          "filter": [
            "lowercase"
          ],
          "tokenizer": "keyword",
          "type": "custom"
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "Name": {
        "type": "text",
        "analyzer": "CustomTokenizer",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "Description": {
        "type": "text",
        "analyzer": "CustomTokenizer",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "CreationUser": {
        "type": "text",
        "analyzer": "CustomTokenizer",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "CreationDate": {
        "type": "text",
        "analyzer": "CustomTokenizer",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      }	
	}
  }
}

```

These are the values of the name field in the index -

TESTDOC-000001-C  
T-RT-001  
T-RT-002  
TESTDOC-000001-B  
T-RT-003  
TESTDOC-000001-D

When i sort using Name field asc it gives me this order

```auto
GET default_index001/_search
{
  "query": {
    "Match_all": {
    }
  }, 
  "sort": [
    {
      "Name.keyword": {
        "order": "asc"
      }
    }
  ],
  "size": 100
}

```

when i execute above query it returns the results in this oder.

T-RT-001  
T-RT-002  
T-RT-003  
TESTDOC-000001-B  
TESTDOC-000001-C  
TESTDOC-000001-D

but the same sort using SQL on Database table gives me this order -

TESTDOC-000001-B  
TESTDOC-000001-C  
TESTDOC-000001-D  
T-RT-001  
T-RT-002  
T-RT-003

how do i make elastic to return the oder same as database?

---

<div class="post-metadata">

**Author:** ![Kathleen\_DeRusso](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kathleen_derusso/32/132039_2.png) [@Kathleen\_DeRusso](https://discuss.elastic.co/u/Kathleen_DeRusso)\
**Post date:** [May 28, 2024, 12:29pm UTC](https://discuss.elastic.co/t/sort-not-working-as-expected/360371/2 "2024-05-28T12:29:52Z")

</div>

Hi there @gjahagir - it looks to me from this example that you want the `-` character considered during sorting. You can extend your [custom analyzer](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-custom-analyzer.html) to handle the `-` character using a character filter.

---

<div class="post-metadata">

**Author:** ![gjahagir](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gjahagir/32/125175_2.png) [@gjahagir](https://discuss.elastic.co/u/gjahagir)\
**Post date:** [May 29, 2024, 7:42am UTC](https://discuss.elastic.co/t/sort-not-working-as-expected/360371/3 "2024-05-29T07:42:13Z")

</div>

Thanks @Kathleen_DeRusso i realized that sort order is controlled by database collation setting. For example if the collation setting is set to SQL\_Latin1\_General\_CP1\_CI\_AS the sort order is this way -  
TESTDOC-000001-B  
TESTDOC-000001-C  
TESTDOC-000001-D  
T-RT-001  
T-RT-002  
T-RT-003

if the collation setting is Latin1\_General\_BIN then the sort order is like this -  
T-RT-001  
T-RT-002  
T-RT-003  
TESTDOC-000001-B  
TESTDOC-000001-C  
TESTDOC-000001-D

same as Elasticsearch.

It looks like special characters are treated differently by database collation setting.

Is there any such option in Elastic?

---

<div class="post-metadata">

**Author:** ![Kathleen\_DeRusso](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kathleen_derusso/32/132039_2.png) [@Kathleen\_DeRusso](https://discuss.elastic.co/u/Kathleen_DeRusso)\
**Post date:** [May 29, 2024, 12:07pm UTC](https://discuss.elastic.co/t/sort-not-working-as-expected/360371/4 "2024-05-29T12:07:41Z")

</div>

We don't support this natively out of the box, but you may be able to do what you're looking for via [script based sorting](https://www.elastic.co/guide/en/elasticsearch/reference/current/sort-search-results.html#script-based-sorting) if my previous suggestion on char filters doesn't work for you.

---

<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:** [June 26, 2024, 12:08pm UTC](https://discuss.elastic.co/t/sort-not-working-as-expected/360371/5 "2024-06-26T12:08:03Z")

</div>

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