Suppose I was going to write my own algorithm to sort an array of strings. How would I go about making an algorithm about it?
I understand the general idea of using ascii characters to compare each character of a string.
The part where I'm stuck is to compare the whole string alphanumerically.
I thought maybe I could just get the sum of the whole string, but that's not what it means. The following is the correct order:
aaa
abccccccccccccccccc
ac
but not this regarding the sum:
aaa
ac
abcccccccccccccccc
l.sort(function (a,b) {
let min = Math.min(a.length, b.length);
for (let i = 0; i < min; i++) {
let l = a[i];
let r = b[i];
if (l !== r) {
return l.charCodeAt(0) - r.charCodeAt(0);
}
}
return a.length - b.length;
});