2013-03-16 10 views
13

SO,java Arrays.sort 2d Array

Ich looknig die folgende Anordnung auf den Werten sortieren von [] [0]

double[][] myArr = new double[mySize][2]; 

so für ex, myArr Inhalt ist:

1  5 
13  1.55 
12  100.6 
12.1 .85 

ich möchte es zu bekommen:

1  5 
12  100.6 
12.1 .85 
13  1.55 

Ich möchte dies tun, ohne meine eigene Art implementieren zu müssen. Jede Hilfe wird geschätzt, danke.

+1

Verwenden Sie einen 'Comparator'. –

Antwort

32

Verwendung Arrays#Sort(T[] a, Comparator c) überladene die Vergleicher als zweites Argument annimmt.

double[][] array= { 
{1, 5}, 
{13, 1.55}, 
{12, 100.6}, 
{12.1, .85} }; 

java.util.Arrays.sort(array, new java.util.Comparator<double[]>() { 
    public int compare(double[] a, double[] b) { 
     return Double.compare(a[0], b[0]); 
    } 
}); 
+3

Diese Subtraktion wird wahrscheinlich nicht funktionieren; Sie sollten stattdessen 'Double.compare (b [0], a [0])' verwenden. –

+0

@LouisWasserman ahh, wahr, bearbeitet es dank :) – PermGenError

+2

Wie können Sie 2D-Array als Argument übergeben, wenn Art ein Array 1D erwartet? Das hat nicht für mich funktioniert. – user2441441

6

Sie benötigen ein Comparator<Double[]> wie so umzusetzen:

public static void main(String[] args) throws IOException { 
    final Double[][] doubles = new Double[][]{{5.0, 4.0}, {1.0, 1.0}, {4.0, 6.0}}; 
    final Comparator<Double[]> arrayComparator = new Comparator<Double[]>() { 
     @Override 
     public int compare(Double[] o1, Double[] o2) { 
      return o1[0].compareTo(o2[0]); 
     } 
    }; 
    Arrays.sort(doubles, arrayComparator); 
    for (final Double[] arr : doubles) { 
     System.out.println(Arrays.toString(arr)); 
    } 
} 

Ausgang:

[1.0, 1.0] 
[4.0, 6.0] 
[5.0, 4.0] 
-1

Für eine allgemeine Lösung, die Sie die Column Comparator verwenden können. Der Code, um die Klasse zu verwenden wäre:

Arrays.sort(myArr, new ColumnComparator(0)); 
0
import java.util.*; 

public class Arrays2 
{ 
    public static void main(String[] args) 
    { 
     int small, row = 0, col = 0, z; 
     int[][] array = new int[5][5]; 

     Random rand = new Random(); 
     for(int i = 0; i < array.length; i++) 
     { 
      for(int j = 0; j < array[i].length; j++) 
      { 
       array[i][j] = rand.nextInt(100); 
       System.out.print(array[i][j] + " "); 
      } 
      System.out.println(); 
     } 

     System.out.println("\n"); 


     for(int k = 0; k < array.length; k++) 
     { 
      for(int p = 0; p < array[k].length; p++) 
      { 
       small = array[k][p]; 
       for(int i = k; i < array.length; i++) 
       { 
        if(i == k) 
         z = p + 1; 
        else 
         z = 0; 
        for(;z < array[i].length; z++) 
        { 
         if(array[i][z] <= small) 
         { 
          small = array[i][z]; 
          row = i; 
          col = z; 
         } 
        } 
       } 
      array[row][col] = array[k][p]; 
      array[k][p] = small; 
      System.out.print(array[k][p] + " "); 
      } 
      System.out.println(); 
     } 
    } 
} 

Good Luck

2

Obwohl dies ein alter Thread, hier sind zwei Beispiele für das Problem in Java8 zu lösen.

durch die erste Spaltensortierung ([] [0]):

double[][] myArr = new double[mySize][2]; 
// ... 
java.util.Arrays.sort(myArr, java.util.Comparator.comparingDouble(a -> a[0])); 

die Sortierung nach den ersten beiden Spalten ([] [0], [] [1]):

double[][] myArr = new double[mySize][2]; 
// ... 
java.util.Arrays.sort(myArr, java.util.Comparator.<double[]>comparingDouble(a -> a[0]).thenComparingDouble(a -> a[1])); 
11

Willkommen Java 8:

Arrays.sort(myArr, (a, b) -> Double.compare(a[0], b[0]));