• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sorting multi-dimension arrays  RSS feed

 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there something in Java that will sort a multi-dimension array? ex:


The first dimension has what I want to sort on, but the other two need to be kept in sync. ex:



The sort would be on year, but the other two dimensions need to be kept with the year. ex:



Please don't post a sort algorithm, I have plenty of them, I just wanted to know if there was something already in Java to do this.

Thanks,

Mike
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there something in Java that will sort a multi-dimension array?


I think you need to elaborate on what you mean by multi-dimensional arrays. Java just have arrays that use an int index -- and support more than one dimension by having arrays of arrays.

You cannot sort an index -- you can only sort the value. What you are describing looks like three arrays that you want to be kept in sync (not a multidimensional array).

Can you give us an example of this structure?

Henry
 
Bartender
Posts: 4002
42
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To store the values you have mentioned you don't need the following declaration,


but


would be enough. Because "[1997][male][23] " are contents your array will contain ?
The sort would be on year, but the other two dimensions need to be kept with the year.

You are refering gender & age as dimensions ? If so they are not.
 
Mike Lipay
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, terminology is lacking, but that is the general idea. I need to sort on year, but keep the other information in sync. Can this be done with existing Java?
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Lipay wrote:I just wanted to know if there was something already in Java to do this.


No, because there is (in most cases) no need for it.
If you have a number of pieces of data that need to be kept together then you should create a new class. You can then use a single dimension array to store instances of that class. You would need to have the class extend the Comparable interface (or write a separate Comparator class) in order to be able to sort the array.
 
Mike Lipay
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
 
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In addition to what Joanne said, check out
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!