• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sorting in Decesinding order  RSS feed

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey yo,

I need to create a map object that stores the key and the value, but it has to sort the values by decesinding order,I was thinking of making a treemap to sort it in accesinding order to start with, but i need to sort the Value, not the key. I dont know much on the subject of sorting and searching so any help would be greatly appericated.
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It really depends on why and how often you need access to the sorted values. If you are going to modify the Map a lot and only rarely access the sorted values, then you could build a sorted set each time. If you need frequent access, you could maintain two collections: the Map and a sorted List.

If you go the latter route, I'd suggest creating a class to manage the two collections together to keep them in-sync and abstract the operations you need.

If you are going to build a sorted list from the values, don't forget that you can get the Collection of values using Map.values() rather than getting all the keys and performing lookups.

As you said, you need the values sorted -- not the keys. However, had you needed the keys sorted, you'll be happy to note that you can specify your own Comparator for the TreeMap, including one that sorts in descending order. There would be no need to keep them in ascending order and copy the keys and values in order to sort them descending.
 
Peter Shipway
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey yo,

The program I am attempting to write, accepts users input about car trips, where they started, what the starting k's where what the ending k's are etc. It just prints out a simplifyed version of the users input, sorted depending upon the command line arugments.
 
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Peter,

In your situation, you could have the cake & eat it

Use the TreeMap to store your object & get it to sort your objects by their values. It can be done.

The TreeMap's constructor allows you to pass in your own Comparator. You could implement the Comparator to compare the object's value instead of the key.

HTH.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Cheng Wei Lee:
The TreeMap's constructor allows you to pass in your own Comparator. You could implement the Comparator to compare the object's value instead of the key.

There are two immediate problems with this. To be clear, what I believe Cheng Wei Lee is suggesting is to create a Comparator that will compare fields in the value rather than the key. Since TreeMap only passes in the key, you'll need to add a reference to the value object within the key itself.

First, how do you look up a value using a key? Since you only have a key -- you're looking for the key's mapped value -- how can you put a reference to the value into the key for the Comparator to use?

Second, if you change a value that is already in the TreeMap (at least the part used in the Comparator), it's location in the TreeMap should be changed (so it can be found later), but it certainly will not be changed since the TreeMap isn't notified. You'd need to manually remove and re-add the key/value pair to the map. Actually, remove it, modify the value, and finally re-add it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!