【问题标题】:Sort two arrayLists concurrently同时对两个数组列表进行排序
【发布时间】:2013-07-20 18:20:38
【问题描述】:

假设我有两个 ArrayList:

name: [Four, Three, One, Two]
num:  [4, 3, 1, 2]

如果我这样做:Arrays.sort(num),那么我有:

name: [Four, Three, One, Two]
num:  [1, 2, 3, 4]

有什么方法可以对 num 进行排序并将其也反映在 name 中,这样我最终可能会得到:

name: [One, Two, Three, Four]
num:  [1, 2, 3, 4]

?请帮帮我。我想到了比较器和对象,但几乎不知道它们。

【问题讨论】:

    标签: java arrays sorting arraylist comparator


    【解决方案1】:

    您应该以某种方式将namenum 字段关联 到一个类中,然后获得该特定类的实例列表。在这个类中,提供一个检查数值的compareTo() 方法。如果您对实例进行排序,那么名称字段也将按照您希望的顺序排列。

    class Entity implements Comparable<Entity> {
        String name;
        int num;
        Entity(String name, int num) {
            this.name = name;
            this.num = num;
        }
        @Override
        public int compareTo(Entity o) {
            if (this.num > o.num)
                return 1;
            else if (this.num < o.num)
                return -1;
            return 0;
        }
    }
    

    测试代码可能是这样的:

    public static void main(String[] args) {
        List<Entity> entities = new ArrayList<Entity>();
        entities.add(new Entity("One", 1));
        entities.add(new Entity("Two", 2));
        entities.add(new Entity("Three", 3));
        entities.add(new Entity("Four", 4));
        Collections.sort(entities);
    
        for (Entity entity : entities)
            System.out.print(entity.num + " => " + entity.name + " ");
    }
    

    输出:

    1 => 一 2 => 二 3 => 三 4 => 四

    【讨论】:

      【解决方案2】:

      您可以使用仅包含索引的数组来代替对实际数组进行排序

      a[i] = i for i = 0..n
      

      您可以使用自定义比较器根据您的 numeruc 数组对该数组进行排序。例如

      bool compare( int a, int b ) { return num[a] < num[b]; }
      

      因此,您可以使用这些索引对两个数组进行排序。

      【讨论】:

      • 我实际上有这个确切的东西。但问题是它非常慢。要得到对应的name值,我必须这样做:name.get(numIndex.indexOf(num.get(value)))
      • wont name.get(a[i]) 如果“a”是一个普通数组就足够了,为了获得第 i+1 个最小的元素,我不知道数组列表是否提供常量查找,否则是的,任何涉及排序的事情都会很慢。
      • @gran_profaci 我知道这是一个老问题,但我已经发布了这个技术的实现,用于任意数量的任意类型的Lists。
      【解决方案3】:

      如果您没有重复的元素,那么您可以只使用像TreeMap 这样的排序地图:

      int[] num = {4, 3, 1, 2};
      String[] name = {"Four", "Three", "One", "Two"};
      TreeMap<Integer,String> sortedMap = new TreeMap<Integer,String>();
      for (int i=0; i<num.length; i++) sortedMap.put(num[i], name[i]);
      // Resulting sortedMap: {1=One, 2=Two, 3=Three, 4=Four}
      

      如果您确实有重复的元素,那么这将不起作用,因为地图的键必须是唯一的。

      【讨论】:

        【解决方案4】:

        在某些情况下,创建一个新类只是为了根据给定列表进行多种排序并没有多大意义。我已经创建了一个执行此操作的函数,但我已将代码发布在 another SO post 中,所以我不会重复它。下面是一个如何使用它的示例。


        用法

        以下是如何使用该函数对任意类型的多个列表进行排序的示例:

        // The key can be any type that implements Comparable, Dupes are allowed
        List<Integer> key = Arrays.asList(4, 3, 1, 2, 1);
        
        // List Types do not need to be the same
        List<String> list1 = Arrays.asList("Four", "Three", "One", "Two", "One");
        List<Character> list2 = Arrays.asList('d', 'c', 'a', 'b', 'a');
        
        // Sorts key, list1, list2 using key as the sorting key.
        keySort(key, key, list1, list2);
        

        输出:

        key:   [1, 1, 2, 3, 4]
        list1: [One, One, Two, Three, Four]
        list2: [a, a, b, c, d]
        

        【讨论】:

          猜你喜欢
          • 2014-06-13
          • 2023-01-19
          • 2012-10-20
          • 2023-02-26
          • 2019-06-07
          • 1970-01-01
          • 2020-06-18
          • 2014-11-10
          • 2012-11-20
          相关资源
          最近更新 更多