Go Back   Velocity Reviews > Newsgroups > Java
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

Java - sorting in order of values

 
Thread Tools Search this Thread
Old 08-16-2006, 01:37 PM   #1
Default sorting in order of values


I need to make a collection wherein i can have a key value pair and the
sorting happens in alphabetical order of values. also i want that after
the initial insert of values if i add or remove any value then the
collection should be sorted again in alphabetical order.
- i cant use arraylist because it doesnt have a key value pair.
- i cant use a hashmap because when i iterate the values i get them in
a random order whereas i want them in alphabetical order.
- TreeMap does the sorting in order of keys.
- LinkedHashMap does the sorting in order of insertion. so for the
initial insert it will be fine
but when i add and remove values it will not give me the alphabetical
order.

would appreciate some help on this

thanks
shikha



Shikha
  Reply With Quote
Old 08-16-2006, 01:55 PM   #2
Eric Sosman
 
Posts: n/a
Default Re: sorting in order of values
Shikha wrote:

> I need to make a collection wherein i can have a key value pair and the
> sorting happens in alphabetical order of values. also i want that after
> the initial insert of values if i add or remove any value then the
> collection should be sorted again in alphabetical order.
> - i cant use arraylist because it doesnt have a key value pair.
> - i cant use a hashmap because when i iterate the values i get them in
> a random order whereas i want them in alphabetical order.
> - TreeMap does the sorting in order of keys.
> - LinkedHashMap does the sorting in order of insertion. so for the
> initial insert it will be fine
> but when i add and remove values it will not give me the alphabetical
> order.


Try just putting the (key,value) pairs in an ordinary HashMap.
When you want to visit all the values (or all the pairs) in order
by value, use .values() to extract the values (or .entrySet() to
extract the pairs), sort them, and traverse the sorted data instead
of the original Map.

That might not be suitable for all situations, but works for
many. If it's not the right answer for your predicament, you'll
need to describe your purposes more fully.

--
Eric Sosman
lid


Eric Sosman
  Reply With Quote
Old 08-16-2006, 02:17 PM   #3
Shikha
 
Posts: n/a
Default Re: sorting in order of values

> Try just putting the (key,value) pairs in an ordinary HashMap.
> When you want to visit all the values (or all the pairs) in order
> by value, use .values() to extract the values (or .entrySet() to
> extract the pairs), sort them, and traverse the sorted data instead
> of the original Map.


you mean to say that i should get the values and put them in an
arraylist and then access the values from the arraylist instead of
getting them from the map ?


> That might not be suitable for all situations, but works for
> many. If it's not the right answer for your predicament, you'll
> need to describe your purposes more fully.


what you suggested should i work. it would have been good if i were
able to directly put the values in some collection which does a sorting
of values.
i want to take a list of vendors from the database alongwith their ids
and store them in a collection. then i want to keep adding and removing
values from this collection based on some user inputs.

Thanks
shikha



Shikha
  Reply With Quote
Old 08-16-2006, 02:25 PM   #4
Shikha
 
Posts: n/a
Default Re: sorting in order of values

> > Try just putting the (key,value) pairs in an ordinary HashMap.
> > When you want to visit all the values (or all the pairs) in order
> > by value, use .values() to extract the values (or .entrySet() to
> > extract the pairs), sort them, and traverse the sorted data instead
> > of the original Map.

>
> you mean to say that i should get the values and put them in an
> arraylist and then access the values from the arraylist instead of
> getting them from the map ?
>

Eric, I was about to try this out but i realised that this wouldnt
work. Because i need to populate the values from a Map. the reason
being that when the user selects a particular value i need to pick the
corresponding key for the value. so if i populate the values from
arraylist i wouldnt be able to get the corresponding id.

any other suggestions ?

thanks
shikha



Shikha
  Reply With Quote
Old 08-16-2006, 04:06 PM   #5
Eric Sosman
 
Posts: n/a
Default Re: sorting in order of values


Shikha wrote On 08/16/06 09:25,:
>>> Try just putting the (key,value) pairs in an ordinary HashMap.
>>>When you want to visit all the values (or all the pairs) in order
>>>by value, use .values() to extract the values (or .entrySet() to
>>>extract the pairs), sort them, and traverse the sorted data instead
>>>of the original Map.

>>
>>you mean to say that i should get the values and put them in an
>>arraylist and then access the values from the arraylist instead of
>>getting them from the map ?
>>

>
> Eric, I was about to try this out but i realised that this wouldnt
> work. Because i need to populate the values from a Map. the reason
> being that when the user selects a particular value i need to pick the
> corresponding key for the value. so if i populate the values from
> arraylist i wouldnt be able to get the corresponding id.


If you need the (key,value) pairs sorted by value, not
just the values themselves, use theMap.entrySet() to get
them: it gives you a Collection of Map.Entry objects, each
representing one (key,value) pair. Sort that Collection of
pairs using a Comparator that looks at the values.

--




Eric Sosman
  Reply With Quote
Old 08-16-2006, 04:16 PM   #6
Stefan Ram
 
Posts: n/a
Default Re: sorting in order of values
Eric Sosman <> writes:
>If you need the (key,value) pairs sorted by value, not
>just the values themselves, use theMap.entrySet() to get
>them: it gives you a Collection of Map.Entry objects, each
>representing one (key,value) pair. Sort that Collection of
>pairs using a Comparator that looks at the values.


When a value is inserted, it could be inserted into both
collections, keeping their respective invariants: So the
sorted collection will stay sorted (insertation sort).

Just inserting a single value into the correct position
should be faster than to sort anew. Shifting all following
values could be faster if a linked list is used.

However: All this is very low-level. So it might be best to
encapsulate the whole decision and implement it in the most
convenient way at first. Then, when the first copies of the
program are sold and it turns out that this implementation
really is too slow, it still can be optimized for version 1.1.



Stefan Ram
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Kodak Easyshare - manual order probs John Bailey General Help Related Topics 0 01-08-2009 04:37 PM
Checkbox values problem in gridview thanigaimani.thirumalai Software 0 11-09-2007 05:12 AM
Why are TV shows on DVD Out Of Order? mhadley DVD Video 3 09-20-2007 11:56 PM
DVD Verdict reviews: LAW AND ORDER: SPECIAL VICTIMS UNIT: THE FIFTH YEAR and more! DVD Verdict DVD Video 0 11-03-2004 10:12 AM
Viewing order for Batman: The Animated Series? Robert Kaiser DVD Video 11 07-25-2004 03:15 AM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46