Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I sort alphabetically and lower case first

Tags:

javascript

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()

enter image description here

like image 461
newBike Avatar asked Jan 19 '26 15:01

newBike


1 Answers

It seems that String.prototype.localeCompare accepts options, that can be found here. sensitivity: 'case' should achieve what you are looking for.

like image 197
Ruben Rutten Avatar answered Jan 22 '26 04:01

Ruben Rutten



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!