java 先按字母表再按数字对字符串数组排序

qco9c6ql  于 2023-04-19  发布在  Java
关注(0)|答案(4)|浏览(220)

我有一个字符串数组,我有一个数据库的fileterd,输出如下所示

[ABCD XYZ M1210, 
ABCD XYZ M149, 
ABCD XYZ M5130, 
ABCD XYZ N1420, 
ABCD XYZ T11299, 
ABCD XYZ S11044]

我希望我的数组排序如下:

[ABCD XYZ M149, 
ABCD XYZ M1210, 
ABCD XYZ M5130, 
ABCD XYZ N1420,  
ABCD XYZ S11044,
ABCD XYZ T11299]

这是我最不想要的元素

==> String theStringIWant = myStringArray.get(myStringArray.size() - 1);

我需要做的是首先按字母顺序对“XYZ”后面的字母进行排序,然后按数字顺序对后面的字母进行排序,例如ABCD XYZ M1210〈ABCD XYZ M5130,因为5130大于1210。
任何帮助在这里将不胜感激

  • 对java等中合适库的任何引用

干杯

bkhjykvo

bkhjykvo1#

Arrays.sort(array, new Comparator<String>() {
    @Override
    public int compare(String s1, String s2) {
        String stringPart1 = extractStringPart(s1); 
        String stringPart2 = extractStringPart(s2); 
        int result = stringPart1.compare(stringPart2);
        if (result == 0) {
            int intPart1 = extractIntPart(s1);
            int intPart2 = extractIntPart(s2);
            result = Integer.compare(intPart1, intPart2);
        }
        return result;
    }
});

这两个提取方法作为练习。阅读String和/或Matcher的javadoc,了解如何做到这一点。

v8wbuo2f

v8wbuo2f2#

您可以使用Collections.sort对内容进行排序。
同时,写一个比较器进行比较,比如,
1写一个比较器

public class MyComparator implements Comparator {

@Override
public int compare(Object o1, Object o2) {

    String s1 = (String)o1;
    String s2 = (String)o2;
    //String part has 10 characters, It is fixed.
    String strPart1 = s1.substring(0,10); 
    int intPart1 = Integer.valueOf(s1.substring(10));

    String strPart2 = s2.substring(0,10);
    int intPart2 = Integer.valueOf(s2.substring(10));

    int strCompareResult = strPart1.compareTo(strPart2);
    if(0 == strCompareResult )
    {
        return intPart1 - intPart2;
    }
    else
    {
        return strCompareResult;
    }
}

}

2使用Collections.sort和Comparator完成排序

List<String> results = Arrays.asList("ABCD XYZ M1210", "ABCD XYZ M149",
            "ABCD XYZ M5130", "ABCD XYZ N1420", "ABCD XYZ T11299",
            "ABCD XYZ S11044");

    System.out.println("Before sorting... ...");
    System.out.println(results);

    System.out.println("After sorting... ... ");
    Collections.sort(results, new MyComparator());
    System.out.println(results);

控制台中的输出:
在分类之前... ...

[ABCD XYZ M1210, ABCD XYZ M149, ABCD XYZ M5130, ABCD XYZ N1420, ABCD XYZ T11299, ABCD XYZ S11044]

分类后... ...

[ABCD XYZ M149, ABCD XYZ M1210, ABCD XYZ M5130, ABCD XYZ N1420, ABCD XYZ S11044, ABCD XYZ T11299]
jgwigjjp

jgwigjjp3#

这是我最不想要的元素
下面的程序首先提取字符串元素的最后一个整数部分。然后通过比较它们返回。

List<String> results = Arrays.asList("ABCD XYZ M1210", "ABCD XYZ M149",
        "ABCD XYZ M5130", "ABCD XYZ N1420", "ABCD XYZ T11299",
        "ABCD XYZ S11044");

        Collections.sort(results, new Comparator<String>(){

            @Override
            public int compare(String o1, String o2) {
                Integer x1 = Integer.parseInt(o1.substring(o1.lastIndexOf(" ")+2, o1.length()));
                Integer x2 = Integer.parseInt(o2.substring(o2.lastIndexOf(" ")+2, o2.length()));

                return x1.compareTo(x2);

            }
        });

        System.out.println(results);

输出:

[ABCD XYZ M149, 
ABCD XYZ M1210, 
ABCD XYZ N1420, 
ABCD XYZ M5130, 
ABCD XYZ S11044, 
ABCD XYZ T11299]
qgzx9mmu

qgzx9mmu4#

一个问题也可以是reverers它一样排序编号其次是字母我知道这是reverers它但张贴所以任何人都可以得到的想法
预期输出:B01 A02 A03 A04 A10 A11 C13 A20 A112

var items = ["A01", "A112","A02", "A03", "B01","C13","A04","A10", "A11", "A20"];
// console.log(   items.sort());
let obj={}
items.map((ele)=>{
   let y=ele.slice(1)
   // console.log(y);
   obj[parseInt(ele.slice(1))]=ele

})
console.log(obj);
for(let yy in obj)
{
console.log(obj[yy]);
}```

相关问题