Reputation: 2698
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?
Upvotes: 28
Views: 63983
Reputation: 816600
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)
Upvotes: 9
Reputation: 1
basic example for another replace, combined with lowercase :D
<button onclick="myFunction('U')">Try it</button>
<p id="demo"></p>
<script>
function myFunction(val) {
var str = "HELLO WORLD!";
var res = str.toLowerCase().split("o");
var elem = document.getElementById("demo").innerHTML
for(i = 0; i < res.length; i++){
(i > 0)?elem += val + res[i]:elem += res[i];
}
}
</script>
Upvotes: 0
Reputation: 66435
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('');
Upvotes: 37
Reputation: 1265
a working example http://jsfiddle.net/uGwZ3/
var string='ACBacb';
alert(string.split('').sort(caseInsensitiveSort).join(''));
function caseInsensitiveSort(a, b)
{
var ret = 0;
a = a.toLowerCase();b = b.toLowerCase();
if(a > b)
ret = 1;
if(a < b)
ret = -1;
return ret;
}
Upvotes: 1
Reputation: 26183
Use a custom sort function like this:
function customSortfunc(a,b){
var lca = a.toLowerCase(), lcb = b.toLowerCase();
return lca > lcb ? 1 : lca < lcb ? -1 : 0;
}
var string='ACBacb';
alert(string.split('').sort(customSortfunc).join(''));
You can read more about the sort function here: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/sort
Beware: if you use localeCompare
like other answer suggests "u" and "ü" will be sorted together as the same letter, since it disregards all diacritics.
Upvotes: 0