• 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 string array in ArrayList

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I need a small help from your end.

I have a ArrayList, which has string array.

My requirement is to first sort the string array as based on salary and followed with First name.

Ex.
ArrayList will contain below array

String array0 = {"salary","first name", "last name" }
String array1 = {"111", "aaa", "aqaq"}
String array2 = {"333", "bbb", "aqaq"}
String array3 = {"111", "ccc", "aqaq"}

Now the array need to be sorted first based on Salary and then first name.

Finalley return a sorted ArrayList...

Can you please help me achieve this?

Many Thanks,
SKJ
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your best bet is to create a bean instead of putting everything into a string array. Make the bean comparable and you are good to go.
 
Sushil Jain
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Thanks for the reply....

I am getting a ArrayList from a different application, the size of arraylist can vary between 0 to 10 and my need is to display the sorted data.

can you please help me with an example, about the way proposed by you.

Many Thanks,
Sushil Jain
 
Sushil Jain
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

looking forward for your support....



Sushil Jain wrote:Hi

Thanks for the reply....

I am getting a ArrayList from a different application, the size of arraylist can vary between 0 to 10 and my need is to display the sorted data.

can you please help me with an example, about the way proposed by you.

Many Thanks,
Sushil Jain

 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What Sebastian proposed is to create VO class say PersonVO, which will have three fields. You set these fields from your array, arr[0], arr[1], arr[2]. You will have as many objects of PersonVO class as there are number of arrays. This PersonVO class will implement comparator, override compare() in this VO class. Then use Collections utility to sort your VO class. Later you can reconvert them in arrays or whatever. Now I am not giving you exact code. look for those pieces in google. This is clean way to sort them, else you can write C style code and do sorting yourself.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sigh.

There's a really easy way to do that which doesn't involve reorganizing the data: write a class that implements Comparator, and use the static Collections.sort() method that accepts a Comparator to sort your list. The Comparator interface has a single method compare(), and it returns a number less than one if the first argument should come first, and greter than one if the second should come first -- that's easy to write, yes? You just compare the array elements in two arrays. Then the sort() method does all the work for you.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic