This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes ArrayList Custom Sort Order Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ArrayList Custom Sort Order" Watch "ArrayList Custom Sort Order" New topic
Author

ArrayList Custom Sort Order

arunkumar subramanian
Ranch Hand

Joined: Jun 10, 2008
Posts: 32
Hi

How to sort an ArrayList based on custom order.



I want to sort this list like "Two", "Three", "One", "Five", "Four". Comparator works on natural sort order. How can I achive this?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

what is this order? can you logically define how these elements are sorted, or is just an absolute?

what happens when you add in "Six", "Seven" and "Eight"?

I guess what I'm saying is that i don't see what order these are in, so it's kind of hard to come up with a way to tell the JVM to do it...


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
arunkumar subramanian
Ranch Hand

Joined: Jun 10, 2008
Posts: 32
It is an absolute ordering. I have a predefined set of information and the arraylist will contain only that data.



There can be duplicate entries in the arraylist, but there will not be any value apart from these.


[ July 03, 2008: Message edited by: arunkumar subramanian ]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

this may sound dumb, but why don't you just add them in the order you want them?
arunkumar subramanian
Ranch Hand

Joined: Jun 10, 2008
Posts: 32
I just took the core of what I need. I will give a try and see. Thanks for your input.
 
 
subject: ArrayList Custom Sort Order