• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sort ArrayList in predefined order

 
Nilesh Pat
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Tim Cooke
Sheriff
Pie
Posts: 3132
136
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a read through the Oracle tutorials for Object Ordering. That'll show you how do achieve what you want.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49796
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does it say in the properties file?
 
Winston Gutkowski
Bartender
Pie
Posts: 10508
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Nilesh Pat
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10508
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49796
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nilesh Pat
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49796
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic