Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中对ArrayList进行排序_Java_Arraylist - Fatal编程技术网

在Java中对ArrayList进行排序

在Java中对ArrayList进行排序,java,arraylist,Java,Arraylist,但我希望输出像这样 a + b + c = 100 a + b = 100 a = 100 b + c = 100; 我该怎么做呢?试试这个 a = 100 a + b = 100 a + b + c= 100 b + c = 100 Collections.sort(国家列表,新比较器(){ @凌驾 公共整数比较(字符串o1、字符串o2){ //TODO自动生成的方法存根 返回o1.replaceAll(“[^a-zA-z]”),与(o2.replaceAll([^a-zA-z]”,比较)

但我希望输出像这样

a + b + c = 100
a + b = 100
a = 100
b + c = 100;
我该怎么做呢?

试试这个

a = 100
a + b = 100
a + b + c= 100
b + c = 100
Collections.sort(国家列表,新比较器(){
@凌驾
公共整数比较(字符串o1、字符串o2){
//TODO自动生成的方法存根
返回o1.replaceAll(“[^a-zA-z]”),与(o2.replaceAll([^a-zA-z]”,比较);
}
});

很难理解您的订购逻辑。请在你的问题中解释一下。无论如何,由于您使用的不是字母数字顺序,您需要实现自己的比较器。您可以解释它是如何工作的吗?@BiratBadeShrestha您需要了解您的顺序模式,它就像没有任何特殊字符或数字的字母顺序,因此,您需要替换字母以外的字符以获得所需的排序顺序
a = 100
a + b = 100
a + b + c= 100
b + c = 100
Collections.sort(listofcountries, new Comparator<String>() {

            @Override
            public int compare(String o1, String o2) {
                // TODO Auto-generated method stub
                return o1.replaceAll("[^a-zA-z]", "").compareTo(o2.replaceAll("[^a-zA-z]", ""));
            }
        });