This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Sorting a multi-dimensional array Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Sorting a multi-dimensional array" Watch "Sorting a multi-dimensional array" New topic
Author

Sorting a multi-dimensional array

Bill Brasskey
Ranch Hand

Joined: Jun 01, 2004
Posts: 47
Hi

I have a multi-dimensional String array with 3 cols, x rows.
The value in the 3rd col is a number. I want to sort the records (rows)
according to this value. Arrays.sort won't accomodate.
Any nudge in the right direction will be GREATLY appreciated.

-Brasskey
Nick George
Ranch Hand

Joined: Apr 04, 2004
Posts: 815
Well, remember that a multi-dimensional array acts like (is?) an array of arrays. In a[][], a[2] is an array. Consider iterating through the rows, sorting them.
[ December 22, 2004: Message edited by: Nick George ]

I've heard it takes forever to grow a woman from the ground
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Your easiest bet to save having to write your own three-column sort routine would be to create a class to hold the three values. Have it either implement Comparable using the number column (or write a separate Comparator). This way you can use Arrays.sort()
Bill Brasskey
Ranch Hand

Joined: Jun 01, 2004
Posts: 47
Hi:

Thanks much for the replies. I ended up creating a small class that implements
comparable, and was able to achieve the result I was after.

Thanks again,
-Brasskey
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sorting a multi-dimensional array
 
Similar Threads
about hashmap implementation of arraylist
tutorial of 2-dimensional arrays
RegularExpression Pattern
arrays
Enhansed For Loop Please explain me