• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sorting ArrayList with Comparitor Interface for String property

 
Komari raj
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just put necessary functionality in your AddressComparator class. For example if address is a String, it could be written like this:

 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic