• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sort ArrayList in predefined order

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does it say in the properties file?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Hey! Wanna see my flashlight? It looks like this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic