Solved

Best way to sort an ArrayList of objects in java

Posted on 2014-12-23
3
162 Views
Last Modified: 2015-01-07
HI,
I have an ArrayList :

private ArrayList<CountryInfo> _supportedCountryInfoList;

Open in new window


I am sorting it the following way :'

Collections.sort(_supportedCountryInfoList, new Comparator<CountryInfo>() {
@Override
 public int compare(CountryInfo lhs, CountryInfo rhs) {
 return lhs.getCountryName().compareTo(rhs.getCountryName());
 }
 });

Open in new window


I want to know what are the other methods to sort an arrayList. And which is better than other.
Is Collections.sort a good approach to sort ?

Thanks
0
Comment
Question by:Rohit Bajaj
3 Comments
 
LVL 14

Accepted Solution

by:
CPColin earned 500 total points
ID: 40514931
I can't remember ever sorting an ArrayList using a different method than Collections.sort(). It definitely gets the job done well.
0
 
LVL 32

Expert Comment

by:awking00
ID: 40514938
While I am not very familiar with it, you might look into using LambdaJ.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 40514985
That method is fine. Of course you might want to look at having a default natural ordering, implementing Comparable<CountryInfo>

btw - try to avoid underscores in normal variable names in Java
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now