简体   繁体   English

JAVA或JAVA SCRIPT或Idoc SCRIPT字母排序

[英]JAVA or JAVA SCRIPT OR Idoc SCRIPT alphabetical sort

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. ASCII表使用了大多数标准排序功能和插件。 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. “短时间”解决方案:将数据切成3个数组或列表:特殊字符,数字,字符。 (test if is number or is between 'a' and 'Z'). (测试是数字还是在'a'和'Z'之间)。 Sort them with fe 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. 使用Java中的fe Collections.sort或Arrays.sort对它们进行排序,这将对每个集合或数组进行排序,然后将它们附加在一起,但不再进行任何排序。 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'. 这有点乏味,主要是为了防止'100'在'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 az character. 在对任何az字符添加一些“权重”之后,对任何数字组(如数字)进行排序,并按字符代码对其他所有内容进行排序。

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
*/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM