• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Sorting two dimensional array

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic