Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sotring a 2D array - compare() 1

Status
Not open for further replies.

dbrb2

Instructor
Jul 19, 2004
121
GB
Hi,

I have some a 2d array that I want to sort by one of the collums

eg:

Before Sorting: After Sorting:

1 10 9 3
2 3 8 4
3 9 4 8
4 8 3 9
5 4 1 10

I looked at the standard util.array methods, and they didn't seem to work with 2d arrays - you couldn't just specify a key collumn...

Some poeple seem to have sorted it using a comparator, but I'm a bit bogged down. This is my attempt, but I think it is very wrong .... any ideas?

(sum is my 2d array)

Arrays.sort(sum, new Comparator() {
public int compare(Object o1, Object o2) {
return ( (o1[1]).
compareTo( (o2[1])));
}
});
 
your sorting algorithm is placing the smaller number first in each 2d array, then sort on the first element of each 2d array?
 
Basiacally -actuallt its the other way around - I want to sort by the second collumn, but it is the eventual order of the first collum that I am interested in once the second is sorted...
 
Do you need something like this:

Code:
int [][] sum = {{0, 2}, {1, 1}, {2, 0}};

for (int [] ia : sum)
{
	for (int i : ia)
		System.out.print ("\t" + i );
	System.out.println ();
}

Arrays.sort (sum, new Comparator()
{
	public int compare (Object o1, Object o2)
	{
		int [] i1 = (int []) o1;
		int [] i2 = (int []) o2;
		return ((i1[1]) - ((i2[1])));
	}
});

for (int [] ia : sum)
{
	for (int i : ia)
		System.out.print ("\t" + i );
	System.out.println ();
}

seeking a job as java-programmer in Berlin:
 
Yes - that's wonderful! Thanks a lot!

I had to modify it a tad, because I'm using an array of doubles. I just put in a section which did the subtraction, then returned the relavent sign, but basically thanks a lot - I was getting nowhere fast before you replied!

Thanks again for you help,

Ben

Code:
import java.util.Arrays;
import java.util.Comparator;
import java.io.*;

public class sort_test
{

  public static void main(String[] args) {
  
  
    double[][] sum = {
        {
        0, 2}, {
        1, 1.3}, {
        2, 0.6}
    };
  
    for (int r = 0; r < sum.length; r++) {
      for (int c = 0; c < sum[r].length; c++) {
        System.out.print(" " + sum[r][c]);
      }
       System.out.println("");
    }
    System.out.println("");
  
    Arrays.sort(sum, new Comparator() {
      public int compare(Object o1, Object o2) {
        double[] i1 = (double[]) o1;
        double[] i2 = (double[]) o2;
         if( ((i1[1]) -  (i2[1]))<0)
           return -1;
         
         else
           return 1;
      }
    });
  
    for (int r = 0; r < sum.length; r++) {
      for (int c = 0; c < sum[r].length; c++) {
        System.out.print(" " + sum[r][c]);
      }
      System.out.println("");
  
    }
  
  }}
 
One more question ...

Looking at the fact that the compare function looks at :

i1[1] - i2[1]

Is the sort implemented an exchange sort?

Thanks for all your help,

Ben
 
Oh no - I see.

It uses a quick sort - the i1[1] - i2[1] is not neccesarily referring to two consecutive elements in the array, just telling the sort algorithm what to compare during the sort.
 
Hey barkerben, I think stefanwagner deserves a star!! He posted code for you after all.

Tim
 
Done - I hadn't noticed that was possible.

Cheers,

Ben
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top