Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Sorting a multi-dimensional array  RSS feed

 
Bill Brasskey
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!