Reputation: 15002
How do I get the result of the below sorting be Food to Eat
then "FOOD 123".
Apparently, the 2nd lower 'o' should bring Food to Eat to the first item after sorting.
I'm surprised this question is not easy to find the answer by Google. This feat is not included in the javascript standard is also surprising me.
[
"FOOD 123",
"Food to Eat"
].sort((a,b)=>{
return a.localeCompare(b)
})
[
"FOOD 123",
"Food to Eat"
].sort()
Upvotes: 3
Views: 416
Reputation: 386654
You could take a custom sorting approach and separate the characters into groups and then sort the strings of the temporary array.
var array = ["FOOD 123", "Food to Eat", 'banana', 'Banana', 'BANANA'],
result = array
.map((string, index) => ({ index, value: Array.from(string, c => c === c.toLowerCase() ? ' ' + c : c + ' ').join('') }))
.sort((a, b) => a.value.localeCompare(b.value))
.map(({ index }) => array[index]);
console.log(result);
Upvotes: 1
Reputation: 1749
It seems that String.prototype.localeCompare
accepts options, that can be found here. sensitivity: 'case'
should achieve what you are looking for.
Upvotes: 1