Solved

Best way to sort an ArrayList of objects in java

Posted on 2014-12-23
3
160 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 31

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.

743 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

11 Experts available now in Live!

Get 1:1 Help Now