Arrays 如何按升序对第一行、第二行和最后第三行中的数字进行排序,但按顺序保存列?
如何根据列值按升序对第一行、第二行和最后第三行中的数字进行排序。例如,我有以下数组:Arrays 如何按升序对第一行、第二行和最后第三行中的数字进行排序,但按顺序保存列?,arrays,sorting,apex-code,apex,Arrays,Sorting,Apex Code,Apex,如何根据列值按升序对第一行、第二行和最后第三行中的数字进行排序。例如,我有以下数组: 7 |10 |15 | 7 | 7 5 | 0 | 4 | 3 | 3 1 | 4 | 3 | 2 | 4 然后,我需要对第一行进行排序,但按顺序保存列,从而对其进行转换: 7 | 7 | 7 |10 |15 5 | 3 | 3 | 0 | 4 1 | 2 | 4 | 4 | 3 Integer k = column.size() - 1; while (k > 1) {
7 |10 |15 | 7 | 7
5 | 0 | 4 | 3 | 3
1 | 4 | 3 | 2 | 4
然后,我需要对第一行进行排序,但按顺序保存列,从而对其进行转换:
7 | 7 | 7 |10 |15
5 | 3 | 3 | 0 | 4
1 | 2 | 4 | 4 | 3
Integer k = column.size() - 1;
while (k > 1) {
Integer id = 0;
for (Integer j = 1; j <= k; j++)
if (listWithNumbers[0][j] > listWithNumbers[0][id])
id = j;
for (Integer i = 0; i < listWithNumbers.size(); i++) {
Integer max = listWithNumbers[i][id];
listWithNumbers[i][id] = listWithNumbers[i][k];
listWithNumbers[i][k] = max;
}
k -= 1;
}
在第二次迭代中,我应该得到如下结果:
7 | 7 | 7 |10 |15
3 | 3 | 5 | 0 | 4
4 | 2 | 1 | 4 | 3
public Comparator<Number[]> comp = (Number[] a, Number[] b) ->
{
for ( int i = 0; i < a.length; ++i )
if ( !a[i].equals( b[i] ) )
return a[i].compareTo( b[i] );
return 0;
}
最后:
7 | 7 | 7 |10 |15
3 | 3 | 5 | 0 | 4
2 | 4 | 1 | 4 | 3
我有以下代码对第一行进行排序并按顺序保存列:
7 | 7 | 7 |10 |15
5 | 3 | 3 | 0 | 4
1 | 2 | 4 | 4 | 3
Integer k = column.size() - 1;
while (k > 1) {
Integer id = 0;
for (Integer j = 1; j <= k; j++)
if (listWithNumbers[0][j] > listWithNumbers[0][id])
id = j;
for (Integer i = 0; i < listWithNumbers.size(); i++) {
Integer max = listWithNumbers[i][id];
listWithNumbers[i][id] = listWithNumbers[i][k];
listWithNumbers[i][k] = max;
}
k -= 1;
}
整数k=column.size()-1;
而(k>1){
整数id=0;
对于(整数j=1;带数字[0][id]的j列表)
id=j;
对于(整数i=0;i
并且,我尝试重写相同的代码来对第二行进行排序,但排序不正确:
k = column.size() - 1;
while (k > 1) {
Integer id = 0;
for (Integer j = 1; j <= k; j++) {
if (listWithNumbers[0][j-1] == listWithNumbers[0][j])
id = j-1;
for (Integer i = 1; i < listWithNumbers.size(); i++) {
Integer max = listWithNumbers[i][id];
listWithNumbers[i][id] = listWithNumbers[i][k];
listWithNumbers[i][k] = max;
}
}
k -= 1;
}
k=column.size()-1;
而(k>1){
整数id=0;
对于(整数j=1;j如果不需要多次迭代,例如不必打印出步骤,那么只需创建一个检查第一行的比较器,然后如果相同的比较器检查第二行,依此类推
大概是这样的:
7 | 7 | 7 |10 |15
3 | 3 | 5 | 0 | 4
4 | 2 | 1 | 4 | 3
public Comparator<Number[]> comp = (Number[] a, Number[] b) ->
{
for ( int i = 0; i < a.length; ++i )
if ( !a[i].equals( b[i] ) )
return a[i].compareTo( b[i] );
return 0;
}
公共比较器comp=(编号[]a,编号[]b)->
{
对于(int i=0;i
从任何方向(向上或向下)看,您的最终状态似乎未排序。您是否复制了一些数据?您不需要进行多次迭代,只需创建一个检查第一行的比较器
,而不是相同的检查第二行,依此类推……将数组转换为类似于列表
,其中第一个整数是值,第二个整数是原始i然后按值对列表进行排序。然后迭代并使用对中的原始索引在下一个列表中交换。