简体   繁体   中英

Sorting arrays of the same size according to one array

I have n arrays of the same length (say m ). The arrays represent different properties of the same m logical objects and I want to sort all the arrays according to one (or more) property. What's an idiomatic or anyway compact way in Java to do this? I came up with the below, which is very verbose.

To give an idea about what I'm looking for, in Python you can just say zip(size, weight).sort , and other languages allow you to get the rank of the key array(s), which you can then apply to all the arrays.

Use of widespread utilities like Apache Commons welcome, but obviously a solution based on Java libraries would be preferred.

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

No need to create a containing Object unless you just want to wrap an array.

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM