I have the following task:
All items within categories should be sorted alphabetically except for Examples. Special characters and numbers should be displayed before letters.
I'm facing with a problem. Most of standard sort functions and plugins are being used the ASCII t开发者_如何学Pythonable. In this table the following symbols: ~,},{ etc. have the index more than letters,for example: Actual result of sorting is:
1 - #1 A T
2 - A T
3 - {C T
I need to get:
1 - #1 A T
2 - {C T
3 - A T
Please give me your piece of advice or any examples ASAP.
Best Regards.
"short of time" solution : cut data in 3 arrays or lists : special chars, numbers, chars. (test if is number or is between 'a' and 'Z'). Sort them with f.e. Collections.sort or Arrays.sort in Java which will sort each collection or array and then append them together but don't make any sorting anymore. I haven't tested this, but it looks like it might work
This is a little tedious, mostly to keep '100' from sorting before '2'.
You can split the strings into individual characters and groups of digits.
Sort any digit groups like numbers, and sort everything else by character code, after adding some 'weight' to any a-z character.
Array.prototype.a1Sort= function(){
var a1, b1, rx=/(\d+)|(\D)/g, rd=/\d+/;
return this.sort(function(a, b){
a= a.toLowerCase().match(rx);
b= b.toLowerCase().match(rx);
while(a.length && b.length){
a1= a.shift();
b1= b.shift();
if(rd.test(a1) || rd.test(b1)){
if(!rd.test(a1)) return 1;
if(!rd.test(b1)) return -1;
if(a1!= b1) return a1-b1;
}
else{
a1= a1.charCodeAt(0);
b1= b1.charCodeAt(0);
if(a1> 96 && a1<123) a1+= 1000;
if(b1> 96 && b1<123) b1+= 1000;
if(a1!= b1) return a1= b1;
}
}
return a.length-b.length;
});
}
var s1=['#1 A T','A T','{C T'];
alert(s1.customSort())
/* returned value: (Array)
#1 A T,{C T,A T
*/
精彩评论