Sorting an array of hashtables


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Sorting an array of hashtables

  1. #1
    Jim Drohan Guest

    Sorting an array of hashtables


    Is there a way to use the Arrays class, sort method, to sort an array of hashtables
    by a value extracted from each hashtable? I can loop over the array and examine
    all the key:value pairs from each hashtable but I'm not sure how to get the
    key:value I want to sort by from each hashtable to the sort method.

  2. #2
    Paul Clapham Guest

    Re: Sorting an array of hashtables

    According to the Java documentation for method "sort(Object[])" in class
    Arrays, the objects you are sorting must implement the Comparable interface.
    This requires your objects to have a method "public int compareTo(Object
    other)" that returns zero if the object is "equal to" the other object, an
    negative number if it's "less than" the other object, and a positive number
    if it's "greater than" the other object.

    Hashtable doesn't implement Comparable, so the short answer to your question
    is NO. But there are two ways you can do it. One is to wrap Hashtable in
    another class that does implement Comparable, and to have an array of this
    other class. The other is to use the method "sort(Object[], Comparator)"
    and to create another class that implements the Comparator interface to use
    as the second parameter.

    To implement Comparator, your class would have to implement two methods,
    namely "compareTo(Object, Object)" to compare two Hashtables (however you
    decide) and "equals(Object)". Look up the documentation for Comparable and
    Comparator for more info.

    PC2

    Jim Drohan <drohanj@tycoelectronics.com> wrote in message
    news:3a156976$1@news.devx.com...
    >
    > Is there a way to use the Arrays class, sort method, to sort an array of

    hashtables
    > by a value extracted from each hashtable? I can loop over the array and

    examine
    > all the key:value pairs from each hashtable but I'm not sure how to get

    the
    > key:value I want to sort by from each hashtable to the sort method.




  3. #3
    Jim Drohan Guest

    Re: Sorting an array of hashtables


    Thanks Paul I really appreciate your response its very helpful. I think I
    will try your second suggestion. Thanks Again.
    Jim


    "Paul Clapham" <pclapham@core-mark.com> wrote:
    >According to the Java documentation for method "sort(Object[])" in class
    >Arrays, the objects you are sorting must implement the Comparable interface.
    >This requires your objects to have a method "public int compareTo(Object
    >other)" that returns zero if the object is "equal to" the other object,

    an
    >negative number if it's "less than" the other object, and a positive number
    >if it's "greater than" the other object.
    >
    >Hashtable doesn't implement Comparable, so the short answer to your question
    >is NO. But there are two ways you can do it. One is to wrap Hashtable

    in
    >another class that does implement Comparable, and to have an array of this
    >other class. The other is to use the method "sort(Object[], Comparator)"
    >and to create another class that implements the Comparator interface to

    use
    >as the second parameter.
    >
    >To implement Comparator, your class would have to implement two methods,
    >namely "compareTo(Object, Object)" to compare two Hashtables (however you
    >decide) and "equals(Object)". Look up the documentation for Comparable

    and
    >Comparator for more info.
    >
    >PC2
    >
    >Jim Drohan <drohanj@tycoelectronics.com> wrote in message
    >news:3a156976$1@news.devx.com...
    >>
    >> Is there a way to use the Arrays class, sort method, to sort an array

    of
    >hashtables
    >> by a value extracted from each hashtable? I can loop over the array and

    >examine
    >> all the key:value pairs from each hashtable but I'm not sure how to get

    >the
    >> key:value I want to sort by from each hashtable to the sort method.

    >
    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center