• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Soting objects in collection

 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had stored list of objects in an ArralyList or HashMap.
Now how to sort the values from the collection.

Thanks,
aakash
 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
     
    Ranch Hand
    Posts: 177
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    Sheriff
    Posts: 7023
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Moving this to the Intermediate forum...
     
    Ranch Hand
    Posts: 815
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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.
    reply
      Bookmark Topic Watch Topic
    • New Topic