I'm working on a JavaScript (jQuery's OK too if this needs it, but I doubt it will) function to alphabetize a string of letters. Let's say the string that I want to sort is: "ACBacb".
My code as of now is this:
var string='ACBacb'; alert(string.split('').sort().join(''));
This returns ABCabc. I can see why that happens, but that is not the format that I am looking for. Is there a way that I can sort it by putting the same letters next to each other, capital letter first? So when I put in ACBacb, I get AaBbCc?
In JavaScript arrays have a sort( ) method that sorts the array items into an alphabetical order. The sort( ) method accepts an optional argument which is a function that compares two elements of the array. If the compare function is omitted, then the sort( ) method will sort the element based on the elements values.
The sort() method sorts the elements of an array in place and returns the reference to the same array, now sorted. The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values.
Array.sort
can have a sort function as optional argument.
What about sorting the string first (ACBacbA becomes AABCabc), and then sorting it case-insensitive:
function case_insensitive_comp(strA, strB) { return strA.toLowerCase().localeCompare(strB.toLowerCase()); } var str = 'ACBacbA'; // split the string in chunks str = str.split(""); // sorting str = str.sort(); str = str.sort( case_insensitive_comp ) // concatenate the chunks in one string str = str.join(""); alert(str);
As per Felix suggestion, the first sort function can be omitted and merged in the second one. First, do a case-insensitive comparison between both characters. If they are equal, check their case-sensitive equivalents. Return -1 or 1 for a difference and zero for equality.
function compare(strA, strB) { var icmp = strA.toLowerCase().localeCompare(strB.toLowerCase()); if (icmp != 0) { // spotted a difference when considering the locale return icmp; } // no difference found when considering locale, let's see whether // capitalization matters if (strA > strB) { return 1; } else if (strA < strB) { return -1; } else { // the characters are equal. return 0; } } var str = 'ACBacbA'; str = str.split(''); str = str.sort( compare ); str = str.join('');
You can pass a custom comparison function to Array.sort()
The already given answers are right so far that you have to use a custom comparison function. However you have to add an extra step to sort capital letters before lower case once:
function cmp(x,y) { if(x.toLowerCase() !== y.toLowerCase()) { x = x.toLowerCase(); y = y.toLowerCase(); } return x > y ? 1 : (x < y ? -1 : 0); // or return x.localeCompare(y); }
If the letters are the same, the originals have to be compared, not the lower case versions. The upper case letter is always "larger" than the lower case version.
DEMO (based on @Matt Ball's version)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With