I encountered a strange behavior while trying to sort a JavaScript array.
var arr = ['a', 'b', 'C', 'd', 'e', 'f', 'g', 'h', 'I', 'k'];
arr.sort(function (a, b) {
console.log(a, b);
if (a.length < b.length) return 1;
else if (a.length > b.length) return -1;
else return 0;
});
Works fine in this case giving me back the same array.
The console goes like this,
But when I try for this below input,
var arr = ['a', 'b', 'C', 'd', 'e', 'f', 'g', 'h', 'I', 'k', 'l'];
Gives me this,
I can't quite figure out why that is happening.
PS. I am writing this custom sort checking the length of the elements because I need an array that has its elements sorted according to the length.