• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sorting a Multi-Dimensional Array  RSS feed

 
Ian Taylor
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there.

I am currently writing an app that collects data entries from the user (first variable) and ultimately gives them each a corresponding 'score' (second variable) which can change as the program runs. I then need to sort and display the data in order of this score. For example, the raw data could be:

Blue, 6
Red, 1
Green, 4
Yellow, 8

And when sorted it would need to come out as

Yellow, 8
Blue, 6
Green, 4
Red, 1

This seems to be a fairly simple thing that tons of programs must need to do, and in Excel I can do it in seconds, but I can't seem to find an easy way of doing it in Java. My initial thought was to make a multidimensional array, but because this doesn't associate the two 'columns' with each other, it can't really sort properly. And besides, the list won't be a fixed length so an array isn't necessarily suitable. I have tried Googling the problem and have read some ideas for doing this, but they seem extremely clunky and cumbersome. I assume for something so simple there should be a more eloquent solution that I am missing?

Any help appreciated
 
Ian Taylor
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The other main idea I had was to make an arraylist of objects instead. In other words, make a regular one-dimensional array but with each entry being an object containing two instance variables (colour and score in the above example). This works okay to a point, but how on earth would you sort such an array? Is it even possible?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ian Taylor wrote:The other main idea I had was to make an arraylist of objects instead. In other words, make a regular one-dimensional array but with each entry being an object containing two instance variables (colour and score in the above example). This works okay to a point, but how on earth would you sort such an array? Is it even possible?

Sure it is: have a look at the Comparable interface (java.lang.Comparable). Or indeed, Comparator (java.util.Comparator). Then have a look at Collections.sort().

Of course, you'll need to put them into a List rather than an array; but you should probably be doing that anyway. Arrays are naff.

Winston
 
Ian Taylor
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Ian Taylor wrote:The other main idea I had was to make an arraylist of objects instead. In other words, make a regular one-dimensional array but with each entry being an object containing two instance variables (colour and score in the above example). This works okay to a point, but how on earth would you sort such an array? Is it even possible?

Sure it is: have a look at the Comparable interface (java.lang.Comparable). Or indeed, Comparator (java.util.Comparator). Then have a look at Collections.sort().

Of course, you'll need to put them into a List rather than an array; but you should probably be doing that anyway. Arrays are naff.

Winston


Just done a few hours of research and experimentation and I think I kind of get it now. Thanks for the pointer!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ian Taylor wrote:Thanks for the pointer!

You're most welcome.

Winston
 
Abhishk Singh
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using comparator/comparable is the best way to go for it. Alternatively, if the requirement is to sort such data in naturally ascending order of input elements, then TreeMap can be used. Also, if required LinkedHashMap can be used. LinkedHashMap retains the order in which elements are inserted into it.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhishk Singh wrote:Alternatively, if the requirement is to sort such data in naturally ascending order of input elements, then TreeMap can be used.

Except that that would mean all scores would have to be unique.

Abhishk Singh wrote:Also, if required LinkedHashMap can be used. LinkedHashMap retains the order in which elements are inserted into it.

But we already know that the insertion order is not what is required, so a LinkedHashMap is not an option here.
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You ca nfind out all about collections in the Java Tutorials. You will probably find the “object ordering” section most useful at this moment.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!