Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 181
  • Last Modified:

Best way to sort an ArrayList of objects in java

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
Rohit Bajaj
Asked:
Rohit Bajaj
1 Solution
 
CPColinCommented:
I can't remember ever sorting an ArrayList using a different method than Collections.sort(). It definitely gets the job done well.
0
 
awking00Commented:
While I am not very familiar with it, you might look into using LambdaJ.
0
 
CEHJCommented:
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now