I would need to sort an array of letters considering the case of each string, to be more specific, I want upper case letters to go first, how can I do this? I found a solution that handles this but there is one bug that I don`t know how to fix:
var arr = ["T", "h", "e", "c", "a", "t", "s", "a", "t", "o", "n", "t", "h", "e", "m", "a", "t"];
arr.sort(function(a, b)
{
var x = a.toLowerCase(), y = b.toLowerCase();
if(x < y) {return -1}
else if (x > y ) {return 1}
else {return 0};
});
Why won`t the capital t be first on the list of 'T'`s, like this :
["a", "a", "a", "c", "e", "e", "h", "h", "m", "n", "o", "s", "T", "t", "t", "t", "t"]
P.S I am sorry for previous confusing question. I am trying to sort an array according to the case.
What I have tried:
.sort(function (a, b) {
return a.toLowerCase().localeCompare(b.toLowerCase());
});
The output is the same