Sorting 如何对列表进行排序<;列表<;整数>&燃气轮机;基于每个内部列表中元素的顺序?

Sorting 如何对列表进行排序<;列表<;整数>&燃气轮机;基于每个内部列表中元素的顺序?,sorting,Sorting,例如,我有一个列表,如 [[0, 5, 9],[0, 3, 5], [0, 3, 7], [0, 5, 7], [0, 3, 9], [0, 7, 9], [3, 5, 7]] 预期结果应该是这样的 [[0,3,5],[0,3,7],[0,3,9],[0,5,7],[0,5,9],[0,7,9],[3,5,7]。 排序基于数组中的每个元素。所以[0,3,9]应该在[0,5,7]之前 非常感谢所有可能的解决方案 创建一个比较两个列表的比较函数,然后将自定义比较函数传递给排序函数。创建一个比较两个

例如,我有一个列表,如

[[0, 5, 9],[0, 3, 5], [0, 3, 7], [0, 5, 7], [0, 3, 9], [0, 7, 9], [3, 5, 7]]
预期结果应该是这样的

[[0,3,5],[0,3,7],[0,3,9],[0,5,7],[0,5,9],[0,7,9],[3,5,7]。

排序基于数组中的每个元素。所以[0,3,9]应该在[0,5,7]之前


非常感谢所有可能的解决方案

创建一个比较两个列表的比较函数,然后将自定义比较函数传递给排序函数。

创建一个比较两个列表的比较函数,然后将自定义比较函数传递给排序函数。

我的第一个想法是覆盖比较器并比较列表中的每个元素

Collections.sort(res, new Comparator<ArrayList<Integer>>() {
        @Override
        public int compare(ArrayList<Integer> o1, ArrayList<Integer> o2) {
            if (o1.size() == o2.size()){
               for (int i = 0; i < o1.size(); i++){
                   if (o1.get(i) < o2.get(i)){
                       return -1;
                   }else{
                       return 1;
                   }
               }
            }
            return o1.size() - o2.size();
        }
    });
Collections.sort(res,新的Comparator(){
@凌驾
公共整数比较(ArrayList o1,ArrayList o2){
如果(o1.size()==o2.size()){
对于(int i=0;i
但是,它不适用于[0,3,9,][0,5,7]],因为当我们比较3和5时,[0,3,9]会先到,但当我们比较7和9时,[0,5,7]会先到

因此,我尝试比较字符串而不是整数:

Collections.sort(res, new Comparator<ArrayList<Integer>>() {
        @Override
        public int compare(ArrayList<Integer> o1, ArrayList<Integer> o2) {
            if (o1.size() == o2.size()){
                String s1 = "";
                String s2 = "";
               for (int n1: o1){
                   s1 += n1;
               }
               for (int n2: o2){
                   s2 += n2;
               }
               return s1.compareTo(s2);
            }
            return o1.size() - o2.size();

        }
    });
Collections.sort(res,新的Comparator(){
@凌驾
公共整数比较(ArrayList o1,ArrayList o2){
如果(o1.size()==o2.size()){
字符串s1=“”;
字符串s2=“”;
对于(int n1:o1){
s1+=n1;
}
对于(内部n2:o2){
s2+=n2;
}
返回s1.compareTo(s2);
}
返回o1.size()-o2.size();
}
});

我的第一个想法是重写比较器并比较列表中的每个元素

Collections.sort(res, new Comparator<ArrayList<Integer>>() {
        @Override
        public int compare(ArrayList<Integer> o1, ArrayList<Integer> o2) {
            if (o1.size() == o2.size()){
               for (int i = 0; i < o1.size(); i++){
                   if (o1.get(i) < o2.get(i)){
                       return -1;
                   }else{
                       return 1;
                   }
               }
            }
            return o1.size() - o2.size();
        }
    });
Collections.sort(res,新的Comparator(){
@凌驾
公共整数比较(ArrayList o1,ArrayList o2){
如果(o1.size()==o2.size()){
对于(int i=0;i
但是,它不适用于[0,3,9,][0,5,7]],因为当我们比较3和5时,[0,3,9]会先到,但当我们比较7和9时,[0,5,7]会先到

因此,我尝试比较字符串而不是整数:

Collections.sort(res, new Comparator<ArrayList<Integer>>() {
        @Override
        public int compare(ArrayList<Integer> o1, ArrayList<Integer> o2) {
            if (o1.size() == o2.size()){
                String s1 = "";
                String s2 = "";
               for (int n1: o1){
                   s1 += n1;
               }
               for (int n2: o2){
                   s2 += n2;
               }
               return s1.compareTo(s2);
            }
            return o1.size() - o2.size();

        }
    });
Collections.sort(res,新的Comparator(){
@凌驾
公共整数比较(ArrayList o1,ArrayList o2){
如果(o1.size()==o2.size()){
字符串s1=“”;
字符串s2=“”;
对于(int n1:o1){
s1+=n1;
}
对于(内部n2:o2){
s2+=n2;
}
返回s1.compareTo(s2);
}
返回o1.size()-o2.size();
}
});

为什么这么复杂-它只是
int
s-你可以根据重要性将它们乘以一个合适的10^n,然后相加。这将形成排序键,然后是简单的按整数升序排序:

(C#-您没有指定语言)

输出:

[[0, 5, 9], [0, 3, 5], [0, 3, 7], [0, 5, 7], [0, 3, 9], [0, 7, 9], [3, 5, 7]] => 
    [[0, 3, 5], [0, 3, 7], [0, 3, 9], [0, 5, 7], [0, 5, 9], [0, 7, 9], [3, 5, 7]]

为什么这么复杂呢?它只是
int
s-你可以根据重要性将它们乘以一个合适的10^n,然后相加。这将形成排序键,然后是简单的按整数升序排序:

(C#-您没有指定语言)

输出:

[[0, 5, 9], [0, 3, 5], [0, 3, 7], [0, 5, 7], [0, 3, 9], [0, 7, 9], [3, 5, 7]] => 
    [[0, 3, 5], [0, 3, 7], [0, 3, 9], [0, 5, 7], [0, 5, 9], [0, 7, 9], [3, 5, 7]]

我的第一个排序是覆盖comparator并比较列表中的每个元素。你知道comparator,那么你的问题是什么?没有给出编码语言-我发布了python&c的答案-诀窍是将你的内部列表组合成一个简单的整数,然后按它排序:[0,3,5]=>35,[0,3,7]=>37,[3,5,7]=>300+50+7=357等等。我的第一个排序是覆盖比较器并比较列表中的每个元素。你知道比较器,那么你的问题是什么?没有给出编码语言-我发布了python&c的答案-诀窍是将你的内部列表组合成一个简单的整数,然后按它排序:[0,3,5]=>35,[0,3,7]=>37,[3,5,7]=>300+50+7=357等。