Solved

Best way to sort an ArrayList of objects in java

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to get all the API from website? 11 91
Error trying to install RTMT Win7 5 60
fibonacci ten numbers 4 39
eclipse buid path vs tomcat lib path 10 23
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.

809 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