You're getting results in lexicographical order which is perfectly fine for a computer but does not make much sense to human beings (expecting results to be sorted in alphabetical order).
The bytes used to represent capital letters have a lower value than the bytes used to represent lowercase letters, and so the names are sorted with the lowest bytes first. ASCII Table
To achieve this, you need to index each name in a way that the byte ordering corresponds to the sort order that you want. In other words, you need an analyzer that will emit a single lowercase token.
Create a custom keyword analyzer for the field you want to sort:
PUT /my_index
{
"settings" : {
"analysis" : {
"analyzer" : {
"custom_keyword_analyzer" : {
"tokenizer" : "keyword",
"filter" : ["lowercase"]
}
}
}
},
"mappings" : {
"_doc" : {
"properties" : {
"name" : {
"type" : "text",
"fields" : {
"raw" : {
"type" : "text",
"analyzer" : "custom_keyword_analyzer",
"fielddata": true
}
}
}
}
}
}
}
Index your data:
POST my_index/_doc/1
{
"name" : "Company 01"
}
POST my_index/_doc/2
{
"name" : "Company 02"
}
POST my_index/_doc/3
{
"name" : "aa 01"
}
POST my_index/_doc/4
{
"name" : "aabb"
}
Perform sort:
POST /my_index/_doc/_search
{
"sort": "name.raw"
}
Response:
[
{
"_index": "my_index",
"_type": "_doc",
"_id": "3",
"_score": null,
"_source": {
"name": "aa 01"
},
"sort": [
"aa 01"
]
},
{
"_index": "my_index",
"_type": "_doc",
"_id": "4",
"_score": null,
"_source": {
"name": "aabb"
},
"sort": [
"aabb"
]
},
{
"_index": "my_index",
"_type": "_doc",
"_id": "1",
"_score": null,
"_source": {
"name": "Company 01"
},
"sort": [
"company 01"
]
},
{
"_index": "my_index",
"_type": "_doc",
"_id": "2",
"_score": null,
"_source": {
"name": "Company 02"
},
"sort": [
"company 02"
]
}
]
Reference: Sorting and Collations