aspose file tools*
The moose likes Beginning Java and the fly likes Sorting two dimensional array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Sorting two dimensional array" Watch "Sorting two dimensional array" New topic
Author

Sorting two dimensional array

Vic Newman
Ranch Hand

Joined: Mar 01, 2002
Posts: 33
Is it possible to sort a 2 dimensional array with Arrays.sort()? It works great with a one dimensional array. I have a simple 2 dimensional array of strings [5] [10]. I want to sort on the first element.
Neil Laurance
Ranch Hand

Joined: Jul 18, 2002
Posts: 183
I don't think you can directly use sort, but someting like this should be a good starting point:

According to my Java in a Nutshell, in most cases the Comparator.compare method should be implemented to return 0 only when o1.equals(o2) returns true. My example doesn't abide by that rule, so there may be a better way to sort this...
Cheers, Neil
Tom Hughes
Ranch Hand

Joined: Feb 09, 2002
Posts: 86
a simple way to implement the equals method would be

I often do this with Comparators, it is not the most efficient but it is good enough 99% of cases.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Neil Laurance:
I don't think you can directly use sort[...]

You can - there is also an Array.sort method that gets a Comparator.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Andrew Lit
Ranch Hand

Joined: Jul 01, 2002
Posts: 135
hi,
i would be appreciated if someone explained me what this line means and what it does?
also how it is concerned with the array, which you want to sort?

thanx
Neil Laurance
Ranch Hand

Joined: Jul 18, 2002
Posts: 183
Originally posted by Ilja Preuss:
You can - there is also an Array.sort method that gets a Comparator.

In that case, you can use:

Cheers, Neil
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sorting two dimensional array