JAVA 或 JAVA SCRIPT 或 Idoc SCRIPT 按字母顺序排序

发布于 2024-09-02 01:58:45 字数 300 浏览 3 评论 0原文

我有以下任务:

类别中的所有项目都应按字母顺序排序,示例除外。特殊字符和数字应显示在字母之前。

我面临着一个问题。大多数标准排序函数和插件都使用 ASCII 表。此表中以下符号:~、}、{等的索引多于字母,例如: 排序的实际结果是:

1 - #1 A T
2 - A T
3 - {C T

我需要得到:

1 - #1 A T
2 - {C T
3 - A T 

请尽快给我你的建议或任何例子。

此致。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北陌 2024-09-09 01:58:45

“时间不够”的解决方案:将数据切割成 3 个数组或列表:特殊字符、数字、字符。 (测试是否为数字或介于“a”和“Z”之间)。使用 Java 中的 fe Collections.sort 或 Arrays.sort 对它们进行排序,这将对每个集合或数组进行排序,然后将它们附加在一起,但不再进行任何排序。我还没有测试过这个,但看起来它可能有效

"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

放手` 2024-09-09 01:58:45

这有点乏味,主要是为了防止“100”排在“2”之前。

您可以将字符串拆分为单个字符和数字组。

对任何数字组(如数字)进行排序,并按字符代码对其他所有内容进行排序,
为任何 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
*/

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
*/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文