VOOZH about

URL: https://mkyong.com/java/how-to-sort-an-arraylist-in-java/

⇱ How to sort an ArrayList in java - Mkyong.com


Skip to content

By default, the ArrayList’s elements are display according to the sequence it is put inside. Often times, you may need to sort the ArrayList to make it alphabetically order. In this example, it shows the use of Collections.sort(‘List’) to sort an ArrayList.


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class SortArrayList{
	
	public static void main(String args[]){
		
		List<String> unsortList = new ArrayList<String>();
		
		unsortList.add("CCC");
		unsortList.add("111");
		unsortList.add("AAA");
		unsortList.add("BBB");
		unsortList.add("ccc");
		unsortList.add("bbb");
		unsortList.add("aaa");
		unsortList.add("333");
		unsortList.add("222");
		
		//before sort
		System.out.println("ArrayList is unsort");
		for(String temp: unsortList){
			System.out.println(temp);
		}
		
		//sort the list
		Collections.sort(unsortList);
		
		//after sorted
		System.out.println("ArrayList is sorted");
		for(String temp: unsortList){
			System.out.println(temp);
		}
	}
	
}

Output


ArrayList is unsort
CCC
111
AAA
BBB
ccc
bbb
aaa
333
222
ArrayList is sorted
111
222
333
AAA
BBB
CCC
aaa
bbb
ccc

Reference

  1. ASCII table list
  2. Collections.sort() documentation

mkyong

Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities.

19 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
infoj
10 years ago

One thing to note here is that sorting will be done in natural ordering and it happens because String class implements Comparable interface and provides implementation for the method compareTo(String anotherString) .. If there is a need to use any other order for sorting then we have to use custom comparator or there is also reverseOrder method.

See some of the ways to sort arraylist here – http://netjs.blogspot.com/2015/08/how-to-sort-arraylist-in-java.html

http://netjs.blogspot.com/2015/08/how-to-sort-arraylist-of-custom-objects-java.html

1
Reply
Sushil Kumar
13 years ago

Can you please tell me which sorting algorithm is used by this function?

1
Reply
atul
13 years ago
Reply to  Sushil Kumar

Its natural sorting according to ASCII

1
Reply
Prashanth Nayak
8 years ago

I have an Employee table with Id, name, positions, salary. i had used comparable and compare methods to sort the fields but is there any way to sort the without comparable method. like i wanna sort positions in the list?

0
Reply
Prashanth Nayak
8 years ago

Positions means Designations and type String

0
Reply
Balack 2015
10 years ago

Hi,I am trying to make the program sort and display in ascending alphabetical order names and also person can search the name on the list.It is easy way to make this with binary file. Check the link please.What should I do to make that happen?
Thank you.http://pastebin.com/bnMHMRW9

0
Reply
papajo
11 years ago

Does it work on duplicates? I think not.

0
Reply
Kenneth
12 years ago

Works like a charm.. Without implementing the comparable interface… Thanks mate.

0
Reply
RickJames!
12 years ago

This made my day.

0
Reply
Bharat Patidar
12 years ago

Man you are awesome, I love you.

0
Reply
Niroshan
12 years ago

Thanks for the post.

How can I validate whether the unsorted ArrayList and sorted list are matching with each other?

0
Reply
Learner
12 years ago

Your tutorials are the best.Simple and straight to the point.Nice job.

0
Reply
Talib Hassan
13 years ago
0
Reply
leandro
13 years ago

Muchas Gracias!

0
Reply
jonny
13 years ago

hey can u tell me how to sort an array list alphabetically using the bubble sort

thanks

0
Reply
Diljeet
13 years ago

Thanks, very much Helped a lot

0
Reply
knrr
15 years ago

should’ve mentioned that the elements in the List have to implement the Comparable interface

0
Reply
atul
13 years ago
Reply to  knrr

knrr, its simple and similar to the arrays sorting post -check out
https://mkyong.com/java/java-object-sorting-example-comparable-and-comparator/

1
Reply
john
12 years ago

this is doesn’t work
I have to do it like that
Collections.sort(student, new Comparator() {

@Override
public int compare(Student o1, Student o2) {
return o1.getName().compareTo(o2.getName());

}
});
}

then it works

-1
Reply