aspose file tools*
The moose likes Beginning Java and the fly likes Sort ArrayList in predefined order Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Sort ArrayList in predefined order " Watch "Sort ArrayList in predefined order " New topic
Author

Sort ArrayList in predefined order

Nilesh Pat
Ranch Hand

Joined: Mar 01, 2010
Posts: 46
I need to arrange my arraylist in a predefined order (Not alphabetical).
i have a list coming from backend and i have set it in order as defined in .properties file.

Can we do it using Comparator?
If so, how? Do any one have an example?


Regards,
Nil
Tim Cooke
Bartender

Joined: Mar 28, 2008
Posts: 1010
    
  47

Have a read through the Oracle tutorials for Object Ordering. That'll show you how do achieve what you want.


Tim Driven Development
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
What does it say in the properties file?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7781
    
  21

Nilesh Pat wrote:I need to arrange my arraylist in a predefined order (Not alphabetical).
Can we do it using Comparator?

Yes.

i have a list coming from backend and i have set it in order as defined in .properties file.

Now that may be a bit more difficult. Perhaps you could explain how the order is held in a properties file?

If it's just a "name" that describes the order, you might want to consider an enum of Comparators (enums are allowed to implement interfaces).

However, first things first: Follow Tim's advice, then set up one Comparator and test it thoroughly.

Once (and only once) you're happy that you understand how they work, then worry about dealing with an order from a properties file.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Nilesh Pat
Ranch Hand

Joined: Mar 01, 2010
Posts: 46
Campbell Ritchie wrote:What does it say in the properties file?


properties file will have order in which we need to arrange the list.
For eg.

0=pritam
1=vishal
2=nikhil
3=nilesh
4=prasanna
5=vijay

list from host will come in any order, it may have more names or less. but we need to order it in as given in this file. extra name will be listed at the end.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7781
    
  21

Nilesh Pat wrote:properties file will have order in which we need to arrange the list.
For eg.
0=pritam...

What, it contains the physical position of every single name you may receive? Seems like a very odd setup to me; but perhaps it's just a class exercise...

So, the order of the names looks like it's defined by the order of their associated "number", which you could determine by putting those values in a Map (java.util.Map) and having your Comparator access it.

However: before you write anything, I'd think very hard about what to do if you get a name that isn't in your list. Simplest is probably just to throw an Exception, but there are other alternatives.

Winston
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
Yes, you can use a Comparator, but you will have to get those data from the configuration file into objects somehow.

You might find it easier to envisage if you rearrange the file so it reads
Gurpreet=0
Anand=1
Madhu=2
etc.
Nilesh Pat
Ranch Hand

Joined: Mar 01, 2010
Posts: 46
I have made the properties file as my own for arranging, as a requirement we got one ordered list saying, list coming from host should be displayed in this order. Which is not alphabetical.
So i saved that list in .properties file giving it's order as key.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11312
    
  16

if you know the order they have to be returned in, why sort it at all? just search through it for the 'next' one you want to return each time, and then return it.

Personally, I'd use a map.


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

Joined: Mar 01, 2010
Posts: 46
fred rosenberger wrote:if you know the order they have to be returned in, why sort it at all? just search through it for the 'next' one you want to return each time, and then return it.

Personally, I'd use a map.



It's like list from host will contain more of less than the known order list. we need to order it.
i can do it playing with arraylist like, creating 3rd list and insert values in it comparing both lists.

I want to know, can we achieve via comparator or any other java functionality?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
Nilesh Pat wrote: . . . I want to know, can we achieve via comparator or any other java functionality?
You have already been given the answer to that question: yes. And suggestions how to do it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sort ArrayList in predefined order