Java - 对多维双数组进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17536859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java - Sort a multidimensional double array
提问by Snay
I have a multidimensional array with double values that I would like to sort..
我有一个多维数组,我想对它进行排序。
//declare array
standingsB = new Double[10][2];
//populate array from the temparray created during read from file
arryLgt = 0;
for (int row = 0; row < standingsB.length; row++){
for (int column = 0; column < standingsB[row].length; column++) {
standingsB[row][column] = Double.parseDouble(tempStandingsArray[arryLgt]);
arryLgt = arryLgt + 1;
}
}
The array has values such as [1.5,7.0] [4.2,4.0] etc...
该数组具有诸如 [1.5,7.0] [4.2,4.0] 等值...
For the next part I don't really know how it works but from reading other articles here this is the best as I can copy without knowledge
对于下一部分,我真的不知道它是如何工作的,但是通过阅读这里的其他文章,这是最好的,因为我可以在没有知识的情况下复制
Arrays.sort(standingsB, new Comparator<Double[]>() {
@Override
public int compare(Double[] s1, Double[] s2) {
compare(s1, s2);
}
});
The above fails to compile (with missing return statement) which is to be expected as I have no idea on how to use the Arrays.sort with a comparator. But I'm not even sure if I'm on the right page being as new to Java (and programing in general) as I am.
以上无法编译(缺少 return 语句),这是意料之中的,因为我不知道如何将 Arrays.sort 与比较器一起使用。但我什至不确定我是否和我一样对 Java(和一般编程)不熟悉。
Thanks for looking!
感谢您的关注!
回答by Tap
You're pretty close. Your comparator will depend on what order you want your results in. Let's say you want the rows to be sorted in the natural order of the first element in each row. Then your code would look like:
你很接近。您的比较器将取决于您希望结果的顺序。假设您希望按每行中第一个元素的自然顺序对行进行排序。然后你的代码看起来像:
Arrays.sort(standingsB, new Comparator<Double[]>() {
public int compare(Double[] s1, Double[] s2) {
if (s1[0] > s2[0])
return 1; // tells Arrays.sort() that s1 comes after s2
else if (s1[0] < s2[0])
return -1; // tells Arrays.sort() that s1 comes before s2
else {
/*
* s1 and s2 are equal. Arrays.sort() is stable,
* so these two rows will appear in their original order.
* You could take it a step further in this block by comparing
* s1[1] and s2[1] in the same manner, but it depends on how
* you want to sort in that situation.
*/
return 0;
}
}
};
回答by Kristian Kraljic
I think the answer provided by @Tap doesn't fulfill the askers question to 100%. As described, the array is sorted for its value at the first index only. The result of sorting {{2,0},{1,2},{1,1}}
would be {{1,2},{1,1},{2,0}}
not {{1,1},{1,2},{2,0}}
, as expected. I've implemented a generic ArrayComparator
for all types implementing the Comparable
interface and released it on my blog:
我认为@Tap 提供的答案并没有 100% 满足提问者的问题。如上所述,数组仅根据其在第一个索引处的值进行排序。排序的结果{{2,0},{1,2},{1,1}}
将{{1,2},{1,1},{2,0}}
不是{{1,1},{1,2},{2,0}}
,正如预期的那样。我已经ArrayComparator
为实现Comparable
接口的所有类型实现了一个泛型,并在我的博客上发布了它:
public class ArrayComparator<T extends Comparable<T>> implements Comparator<T[]> {
@Override public int compare(T[] arrayA, T[] arrayB) {
if(arrayA==arrayB) return 0; int compare;
for(int index=0;index<arrayA.length;index++)
if(index<arrayB.length) {
if((compare=arrayA[index].compareTo(arrayB[index]))!=0)
return compare;
} else return 1; //first array is longer
if(arrayA.length==arrayB.length)
return 0; //arrays are equal
else return -1; //first array is shorter
}
}
With this ArrayComparator
you can sort multi-dimensional arrays:
有了这个,ArrayComparator
您可以对多维数组进行排序:
String[][] sorted = new String[][]{{"A","B"},{"B","C"},{"A","C"}};
Arrays.sort(sorted, new ArrayComparator<>());
Lists
of arrays:
Lists
数组:
List<String[]> sorted = new ArrayList<>();
sorted.add(new String[]{"A","B"});
sorted.add(new String[]{"B","C"});
sorted.add(new String[]{"A","C"});
sorted.sort(new ArrayComparator<>());
And build up (Sorted)Maps
easily:
并(Sorted)Maps
轻松构建:
Map<String[],Object> sorted = new TreeMap<>(new ArrayComparator<>());
sorted.put(new String[]{"A","B"}, new Object());
sorted.put(new String[]{"B","C"}, new Object());
sorted.put(new String[]{"A","C"}, new Object());
Just remember, the generic type must implement the Comparable
interface.
请记住,泛型类型必须实现Comparable
接口。
回答by ?zkan pakdil
Solution with lambda sorting array of int[][] contests example :
使用 int[][] 的 lambda 排序数组的解决方案竞赛示例:
Arrays.sort(contests, (a, b)->Integer.compare(b[0], a[0]));
回答by user2506840
Arrays.sort() expects a single dimensional array while in your case you are trying to pass a multidimensional array.
Arrays.sort() 需要一个一维数组,而在您的情况下,您试图传递一个多维数组。
eg Double[] d = {1.0,5.2,3.2};
例如 Double[] d = {1.0,5.2,3.2};
Then you use Arrays.sort(d) since the sort can work on the primitive types or the wrapper types.
然后你使用 Arrays.sort(d) 因为排序可以在基本类型或包装类型上工作。