简体   繁体   English

排序 2d 整数数组 Java

[英]Sorting 2d Integers Array Java

i have an 2d Array which is [2][10].我有一个二维数组,它是 [2][10]。 It is filled with random numbers.它充满了随机数。 Could someone please help me how to sort it?有人可以帮我如何排序吗? I dont know why but any found way to sort it with Comparator doesnt work for me.我不知道为什么,但是任何找到的使用 Comparator 对其进行排序的方法对我都不起作用。 So im trying to do that in a loop.所以我试图在循环中做到这一点。 It has to be sorted by the column.它必须按列排序。

Im trying to play with creating a temporary array and inserting the value into it, but i dont know how to compare values: [0][0] and [0][1].我试图创建一个临时数组并将值插入其中,但我不知道如何比较值:[0][0] 和 [0][1]。 My try is do 2 for loops and inside it:我的尝试是在循环中执行 2 个循环:

for (int i = 0; i < arr4.length; i++) {
    for(int j = 0; j < arr4[i].length; j++) {
        if(arr4[i][j] > ???????)
    temparray = arr4[i][j];
    }
 }

Help me please...请帮帮我...

if you need to sort line by line you need to update your solution like this如果您需要逐行排序,则需要像这样更新您的解决方案

  for (int i = 0; i < arr4.length; i++) {
        for(int j = 0; j < arr4[i].length-1; j++) {
            if(arr4[i][j] > arr4[i][j+1]){
              temparray = arr4[i][j];
              arr4[i][j] = arr4[i][j+1]
              arr4[i][j+1] = temparray  ;
              j=0;
          }
        }
     }


input:
[1.0, 2.0]
[6.0, 4.0]
[5.0, 4.0]

output:输出:

[1.0, 2.0]
[4.0, 6.0]
[4.0, 5.0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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