Solved

Best way to sort an ArrayList of objects in java

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Application launch issue with Apache Tomcat 5 62
windows explorer path to command prompt 5 54
Java: The Public Class Main 4 30
java mysql insert application 14 25
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

789 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