1

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 table. 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.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
user341203
  • 29
  • 1
  • 7
  • You say "All items within categories should be sorted alphabetically except for " but do not finish the sentence. We can't help you unless we know what the exception(s) is/are. Could you elaborate? – Pops May 15 '10 at 16:24
  • Sorry for misunderstanding. Examples - it is a category of items. So it doesn't change the main sence of the task. "All items within categories should be sorted alphabetically" – user341203 May 17 '10 at 06:40

2 Answers2

0

"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

Xorty
  • 18,367
  • 27
  • 104
  • 155
0

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
*/
kennebec
  • 102,654
  • 32
  • 106
  • 127