【问题标题】:Sorting arrays of the same size according to one array根据一个数组对相同大小的数组进行排序
【发布时间】:2014-08-22 01:21:16
【问题描述】:

我有相同的lengthn 数组(比如m)。数组代表相同m 逻辑对象的不同属性,我想根据一个(或多个)属性对所有数组进行排序。在 Java 中,什么是惯用的或无论如何紧凑的方式来做到这一点?我想出了下面的内容,非常冗长。

要了解我要查找的内容,在 Python 中您可以说 zip(size, weight).sort,而其他语言允许您获取键数组的排名,然后您可以将其应用于所有数组。

欢迎使用广泛的实用程序,如 Apache Commons,但显然基于 Java 库的解决方案更受欢迎。

import java.util.*;

class Box {
  public int size;
  public int weight;

  Box( int size, int weight ){
    this.size = size;
    this.weight = weight;
  }
}

class SizeComparator implements Comparator<Box> {
  @Override
  public int compare(Box a, Box b) {
    return a.size < b.size ? -1 : a.size == b.size ? 0 : 1;
  }
}

public class SortArrays {
  public static void main(String[] args) {
    int[] size   = {5,3,6,2,4,4,2};
    int[] weight = {9,7,8,3,5,2,5};

    List<Box> boxes = new ArrayList<Box>();
    for( int i = 0; i < size.length; ++i ) {
      boxes.add( new Box( size[i], weight[i] ) );
    }

    Collections.sort( boxes, new SizeComparator() );
    int[] sortedSizes = getSizes( boxes );
    int[] sortedWeights = getWeights( boxes );

    System.out.println(Arrays.toString(sortedSizes));
    System.out.println(Arrays.toString(sortedWeights));
  }

  private static int[] getSizes( List<Box> boxes ) {
    int[] result = new int[boxes.size()];
    for( int i = 0; i < boxes.size(); ++i ) {
      result[i] = boxes.get(i).size;
    }
    return result;
  }

  private static int[] getWeights( List<Box> boxes ) {
    int[] result = new int[boxes.size()];
    for( int i = 0; i < boxes.size(); ++i ) {
      result[i] = boxes.get(i).weight;
    }
    return result;
  }
}

【问题讨论】:

  • 创建 POJO 以将属性保存在单个对象中...排序...如果您不能这样做,请创建一个 int 的代理数组,它将保存索引主阵列。排序这个数组,这样当你迭代这个数组时,它看起来好像另一个数组已经排序了......即proxy[0]指向索引5,所以你可以使用size[5]weight[5](或@ 987654332@),这样,主阵列永远不会改变...
  • 为什么要从两个数组开始?你应该有一个Boxes 数组作为开始。
  • Java 中的比较要么由 Comparator 完成(就像您所做的那样),要么通过实现 Comparable 接口完成。与 Python 不同 - Java 很冗长,没有“紧凑”的方式......
  • 如果我是你,我会把它们打包成一个类,然后实现 Comparable 和 Comparator。
  • @MadProgrammer 要对proxy 进行排序,我会使用Arrays.sort( proxy, new CompareUsingThisKey( size ) ) 之类的东西吗,其中CompareUsingThisKey 是一个自定义Comparator&lt;int&gt;,用于存储密钥数组?

标签: java arrays sorting


【解决方案1】:

除非您只想包装一个数组,否则无需创建包含对象。

public static void sortMultiArrays(int[] ... index) {
  // merge arrays
  List<int[]> zip = new ArrayList<int[]>(index[0].length);
  for(int i = 0; i<index[0].length;i++){
    int[] e = new int[index.length];
    for(int j = 0; j<index.length;j++){
      e[j] = index[j][i];
    }
    zip.add(e);
  }
  // sort by first index
  Collections.sort(zip,new ArrayComparator());

  // demerge arrays
  for(int i = 0; i<zip.size();i++) {
    int[] ints = zip.get(i);
    for (int j = 0;j<ints.length;j++) {
      index[j][i] = ints[j];
    }
  }
}

private static class ArrayComparator implements Comparator<int[]>{

  @Override
  public int compare(int[] a, int[] b) {
    return a[0] < b[0] ? -1 : a[0] == b[0] ? 0 : 1;
  }

}

【讨论】:

    猜你喜欢
    • 2014-04-13
    • 2023-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多