GeeCON Prague 2014*
The moose likes Java in General and the fly likes Soting objects in collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Soting objects in collection" Watch "Soting objects in collection" New topic
Author

Soting objects in collection

aakash bhatt
Ranch Hand

Joined: Jan 09, 2003
Posts: 182
I had stored list of objects in an ArralyList or HashMap.
Now how to sort the values from the collection.

Thanks,
aakash
Pat Hays
Ranch Hand

Joined: Aug 20, 2004
Posts: 138
Code is here:

----------
Create your Java GUI application in minutes, http://www.mars3000.com


[ August 27, 2004: Message edited by: Pat Hays ]
[ August 27, 2004: Message edited by: Pat Hays ]

Download Java GUI Builder, <a href="http://www.mars3000.com" target="_blank" rel="nofollow">http://www.mars3000.com</a>
Pete Harris
Ranch Hand

Joined: Feb 05, 2003
Posts: 39
That's the brute force approach, but it assumes all the objects implement the Comparable interface, which is probably valid otherwise they can't be sorted

The other approach is to consider one of the sorted collections such as the TreeMap or TreeSet which sort the data as it is entered and therefore it is always returned in it's natural sorted order. Whether or not you can use one of these classes depends on the design of your application.

cheers, Pete
Mike Reterstorf
Greenhorn

Joined: Aug 06, 2004
Posts: 3
There are several options to sorting objects within a collection. You can attempt to use a Sorted Collection, or write a custom Comparator to use with the Arrays.sort(Object[] a, Comparator c) method to sort the payload of the collection.

1) Sorted Collection

Using a sorted collection requires that all Objects implement the Comparable interface. The purpose for this is that the Sorted Collections (SortedSet, SortedMap, TreeSet, etc.) will call the compareTo(Object a, Object b) of each object that is added to the collection to determine the place it should be placed in the Collection. This is where the natural sort order comes into play. When implementing the Comparable interface you must ensure that the objects are sorted based on their Natural Sort order. For a description of Natural Sort order see the javadoc for java.lang.Comparable. Your implementation of Comparable will determine how your Collection is sorted when using one of the Sorted Collections.

2) Write a custom Comparator

Another option is to write a custom Comparator class which will define the Sort order for a collection of Objects. This is similar to implementing the Comparable interface expect that the logic is imbedded within a Comparator. For more information see the javadoc for java.util.Comparator.

Mike
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Sorry if I seem to be duplicating things, but they aren't as clear from the discussion as they could be.
  • Converting a List to an array and back just to sort it, as in the code above, is unnecessary. Just use java.util.Collections.sort().
  • Such sorting methods are efficient if you need to sort just once. If you want to keep your collection sorted at all times, a TreeSet is usually preferable.
  • In all these cases, you can use either objecs with a natural sorting order (i.e. that implement Comparable), or a Comparator that knows how to compare your object. This works with Arrays.sort(), Collections.sort(), and sorted collections such as TreeSet.
  • If you have an array and you want to turn it into a List, consider using Arrays.asList(). The List returned will have a fixed size though.
  • When implementing Comparator or Comparable, do read the javadoc and follow the contract religously.
  • When implementing Comparable, do make the comparison consistent with equals().
  • When implementing equals(), don't forget to override hashCode().
  • HTH

    - Peter
    Max Rahder
    Ranch Hand

    Joined: Nov 06, 2000
    Posts: 177
    Here's an example for sorting a List. (This is copied from my response to a similar question last month.) Note that you can write the compare routine to compare the values of as many properties as you need. My examples compare a single property, but I could have written them to first compare one value, and in the case of a tie, then compare another property. For example, I could have written one of the routines to first compare name, and if the names were the same, then compare weight.
    Dirk Schreckmann
    Sheriff

    Joined: Dec 10, 2001
    Posts: 7023
    Moving this to the Intermediate forum...


    [How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
    Nick George
    Ranch Hand

    Joined: Apr 04, 2004
    Posts: 815
    Originally posted by Peter den Haan:
    When implementing Comparable, do make the comparison consistent with equals().


    ignore this one at the peril of many braincells wasted on otherwise inexplicable behavior. It's like entering crazyworld.


    I've heard it takes forever to grow a woman from the ground
     
    GeeCON Prague 2014
     
    subject: Soting objects in collection