This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes Sorting ArrayList with Comparitor Interface for String property Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Sorting ArrayList with Comparitor Interface for String property" Watch "Sorting ArrayList with Comparitor Interface for String property" New topic
Author

Sorting ArrayList with Comparitor Interface for String property

Komari raj
Ranch Hand

Joined: Dec 12, 2008
Posts: 43
Hi All

Hi I have list of Beans with 3 proprties of Employee Bean(name, age, address) .

I have Taken Address comparator Class shown in below.

Output I am getting is:

Address: 12-3-hyd Age: 25 Name: XXX
Address: 45-KKD Age: 23 Name: YYY
Address: AD Road-Delhi Age: 56 Name: aaa
Address: BRRoad-Delhi Age: 45 Name: bbb


here my requirement is....

I want to display sort beans as per Address like below..

Address: AD Road-Delhi Age: 56 Name: aaa
Address: BRRoad-Delhi Age: 45 Name: bbb
Address: 12-3-hyd Age: 25 Name: XXX
Address: 45-KKD Age: 23 Name: YYY

That comparior Interface is first preference given to numbers only...but i want write comparitor class for giving first preffs to strings...

Andrey Kozhanov
Ranch Hand

Joined: Mar 12, 2010
Posts: 79
Just put necessary functionality in your AddressComparator class. For example if address is a String, it could be written like this:

Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4366
    
    8

That might work - it depends on what is needed. For instance, that would have 199-XXX come before 1AA-AAA, because it would use alphabetical sorting on the second digit. Whether that is OK depends on the exact requirement (but I suspect it isn't).
Komari raj
Ranch Hand

Joined: Dec 12, 2008
Posts: 43
Andrey Kozhanov wrote:Just put necessary functionality in your AddressComparator class. For example if address is a String, it could be written like this:




Hi Andrey Kozhanov

Thank you very much..

As my requirement it is ok..

Regards
Raju.Komari
 
jQuery in Action, 2nd edition
 
subject: Sorting ArrayList with Comparitor Interface for String property