【问题标题】:How to sort List<List<Integer>> based on the order of element in each inner list?如何根据每个内部列表中元素的顺序对 List<List<Integer>> 进行排序?
【发布时间】:2017-11-20 21:15:08
【问题描述】:

例如,我有一个类似

的列表
[[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] 之前。

非常感谢任何可能的解决方案!

【问题讨论】:

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

标签: sorting


【解决方案1】:

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

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

using System.Linq;
using System.Collections.Generic;
using System;

internal class Program
{
    static void Main(string[] args)
    {
        List<List<int>> l = new List<List<int>> {
            new List<int>{0, 5, 9 },
           new List<int>{0, 3, 5},
           new List<int>{0, 3, 7},
           new List<int>{0, 5, 7},
           new List<int>{0, 3, 9},
           new List<int>{0, 7, 9},
           new List<int>{ 3, 5, 7}
            };

        var sorted = l.OrderBy(sub => sub[0] * 100 + sub[1] * 10 + sub[2]);

        Console.WriteLine("[" + string.Join("],[", sorted.Select(s => string.Join("],[", s))));
        Console.ReadLine();
    }
}

(Python)

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

sorteddata = sorted(data, key= lambda x: x[0]*100+x[1]*10+x[2])
print (data)
print (sorteddata)

输出:

[[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]]

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      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();
              }
          });
      

      但是,它不适用于 case[[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();
      
              }
          });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-03-28
        • 2016-06-26
        • 2021-09-15
        • 2015-01-02
        • 2013-12-29
        • 1970-01-01
        • 1970-01-01
        • 2010-10-17
        相关资源
        最近更新 更多