[英]Nested sorting 2d array java
I have a 2D array with 8 columns (col0-col7) and a thousand rows that I would like to sort ascending on 2 columns: col2 and col3. 我有一个8列(col0-col7)和一千行的2D数组,我想对2列进行升序排序:col2和col3。 I would like to give priority to column col2 and first sort on that column. 我想优先考虑col2列,并对该列进行优先排序。 If two values in col2 are equal (very high chance) then I would like to sort based on col3. 如果col2中的两个值相等(很有可能),那么我想基于col3进行排序。 This is the current code I use: 这是我使用的当前代码:
public static void sortMyArray() {
Arrays.sort(myArray, new Comparator<double[]>() {
public int compare(double[] a, double[] b) {
int result = Double.compare(a[2], b[2]);
if (result == 0) {
return Double.compare(a[3], b[3]);
} else {
return result;
}
}
});
}
The current code I use sorts my array only on col3. 我使用的当前代码仅在col3上对数组进行排序。 I would like to stick to an 2d array due to later calculations. 由于以后的计算,我想坚持使用二维数组。
Hopefully you can help me. 希望你能帮助我。
EDIT: made a few changes based on feedback from a user (for which many thanks!). 编辑:根据用户的反馈进行了一些更改(对此非常感谢!)。 However, the problem remains. 但是,问题仍然存在。 This is the code I used to test the snippet: 这是我用来测试代码段的代码:
public static void loadTotalData() {
for (int i = 0; i < myArray.length; i++) {
myArray[i][0] = classOriginal.allData[i].col0data;
myArray[i][1] = classOriginal.allData[i].col1data;
myArray[i][2] = classOriginal.allData[i].col2data;
myArray[i][3] = classOriginal.allData[i].col3data;
myArray[i][4] = 0.0;
myArray[i][5] = 0.0;
myArray[i][6] = 0.0;
myArray[i][7] = 0.0;
}
System.out.println(myArray[0][3]);
System.out.println(myArray[1][3]);
System.out.println(myArray[2][3]);
System.out.println(myArray[3][3]);
System.out.println(myArray[4][3]);
System.out.println(myArray[5][3]);
System.out.println(myArray[6][3]);
System.out.println(myArray[7][3]);
System.out.println(myArray[8][3]);
System.out.println(myArray[9][3]);
System.out.println(myArray[10][3]);
classForSortingAndLoading.sortMyArray();
System.out.println(myArray[0][3]);
System.out.println(myArray[1][3]);
System.out.println(myArray[2][3]);
System.out.println(myArray[3][3]);
System.out.println(myArray[4][3]);
System.out.println(myArray[5][3]);
System.out.println(myArray[6][3]);
System.out.println(myArray[7][3]);
System.out.println(myArray[8][3]);
System.out.println(myArray[9][3]);
System.out.println(myArray[10][3]);
Probably not the most efficient way to test this, but I know the data source and thus I know what it should give back before and after the sorting. 可能不是测试此问题的最有效方法,但是我知道数据源,因此知道在排序前后应该返回的内容。
You should be comparing columns of a
and b
, rather than a
and a
: 您应该比较a
和b
列,而不是a
和a
:
public static void sortMyArray() {
Arrays.sort(myArray, new Comparator<double[]>() {
public int compare(double[] a, double[] b) {
int result = Double.compare(a[2], b[2]);
if (result == 0) {
return Double.compare(a[3], b[3]);
} else {
return result;
}
}
});
}
Since a[2]
equals a[2]
by definition (identity), result
was always 0
and thus, only column 3 was ever compared, which again would always return 0
( return results
branch of the conditional was never executed). 由于a[2]
在定义(身份)上等于a[2]
, result
始终为0
,因此只比较了列3,而列3始终将返回0
(从不执行条件的return results
分支)。
Example: 例:
public class Main {
public static void main(String[] args) {
double[][] myArray = sortMyArray();
printArray(myArray);
}
public static double[][] sortMyArray() {
double[][] myArray = new double[][] {
{1, 11, 21, 37, 41, 51, 61},
{0, 10, 20, 30, 40, 50, 60},
{2, 12, 22, 32, 42, 52, 62},
{1, 11, 21, 31, 41, 51, 61},
};
Arrays.sort(myArray, new Comparator<double[]>() {
public int compare(double[] a, double[] b) {
int result = Double.compare(a[2], b[2]);
if (result == 0) {
return Double.compare(a[3], b[3]);
} else {
return result;
}
}
});
return myArray;
}
private static void printArray(double[][] myArray) {
for(int row = 0; row < myArray.length; row++){
for( int column = 0; column < myArray[0].length; column++){
System.out.print(myArray[row][column] + " ");
}
System.out.println();
}
}
}
Executing this code results in the following output: 执行此代码将产生以下输出:
0.0 10.0 20.0 30.0 40.0 50.0 60.0
1.0 11.0 21.0 31.0 41.0 51.0 61.0
1.0 11.0 21.0 37.0 41.0 51.0 61.0
2.0 12.0 22.0 32.0 42.0 52.0 62.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.