-
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.
-
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.
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks