aspose file tools*
The moose likes Java in General and the fly likes sort string array in ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "sort string array in ArrayList" Watch "sort string array in ArrayList" New topic
Author

sort string array in ArrayList

Sushil Jain
Greenhorn

Joined: Oct 01, 2009
Posts: 3
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
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
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.


JDBCSupport - An easy to use, light-weight JDBC framework -
Sushil Jain
Greenhorn

Joined: Oct 01, 2009
Posts: 3
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

Joined: Oct 01, 2009
Posts: 3
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
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
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.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

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.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sort string array in ArrayList
 
Similar Threads
sort problem
Sorting an array based on another array
sort a map or arraylist according to the value of the object inside
Sorting Arraylist
Help-> sorting different table columns