How to sort a List in java
Table of Contents
In this article, explaining how to sort a list in java. We can sort list which contains Primitive data types or Wrapper Class Types by using Collections.sort(list) or list.stream().forEach(); We can’t sort list which contains object data types so that we have to use a comparator or comparable interface to sort the list.
Primitive data List Sorting
Create List without any wrapper type and add homogeneous elements instead of heterogeneous elements
Homogeneous Elements
List list=new ArrayList();list.add(3);list.add(6);
Heterogeneous Elements
List list=new ArrayList(); list.add(3); list.add("Hello");
Homogeneous Elements Example
ListPrimitiveDataSorting.java
package com.narayanatutorial.primitive.sorting; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class ListPrimitiveDataSorting { public static void main(String args[]) { List list = new ArrayList(); list.add(123); list.add(34); list.add(567); System.out.println("****Before Sorting****"); list.stream().forEach(name -> System.out.println(name)); Collections.sort(list); System.out.println("****After Sorting****"); list.stream().forEach(name -> System.out.println(name)); list.clear(); list = new ArrayList(); list.add(123); list.add(34); list.add(567); // sorting using stream System.out.println("**** Sorting using list stream ****"); list.stream().sorted().forEach(value -> System.out.println(value)); } }
Integer Wrapper Classe Type List Sorting
ListIntegerSorting.java
package com.narayanatutorial.primitive.sorting; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class ListIntegerSorting { public static void main(String args[]) { List<Integer> list = new ArrayList<Integer>(); list.add(123); list.add(34); list.add(567); System.out.println("****Before Sorting****"); list.stream().forEach(name -> System.out.println(name)); Collections.sort(list); System.out.println("****After Sorting****"); list.stream().forEach(name -> System.out.println(name)); list.clear(); list = new ArrayList<Integer>(); list.add(123); list.add(34); list.add(567); // sorting using stream System.out.println("**** Sorting using list stream ****"); list.stream().sorted().forEach(value -> System.out.println(value)); } }
Ouput
****Before Sorting**** 123 34 567 ****After Sorting**** 34 123 567 **** Sorting using list stream **** 34 123 567
String Type List Sorting
ListStringSorting.java
package com.narayanatutorial.primitive.sorting; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class ListStringSorting { public static void main(String args[]) { List<String> list = new ArrayList<String>(); list.add("Tutorial"); list.add("Narayana"); list.add("Swamy"); System.out.println("****Before Sorting****"); list.stream().forEach(name -> System.out.println(name)); Collections.sort(list); System.out.println("****After Sorting****"); list.stream().forEach(name -> System.out.println(name)); list.clear(); list = new ArrayList<String>(); list.add("Tutorial"); list.add("Narayana"); list.add("Swamy"); // sorting using stream System.out.println("**** Sorting using list stream ****"); list.stream().sorted().forEach(value -> System.out.println(value)); } }
Output
****Before Sorting**** Tutorial Narayana Swamy ****After Sorting**** Narayana Swamy Tutorial **** Sorting using list stream **** Narayana Swamy Tutorial
Long Wrapper Classe Type List Sorting
ListLongSorting.java
package com.narayanatutorial.primitive.sorting; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class ListLongSorting { public static void main(String args[]) { List<Long> list = new ArrayList<Long>(); list.add(123L); list.add(34L); list.add(567L); System.out.println("****Before Sorting****"); list.stream().forEach(name -> System.out.println(name)); Collections.sort(list); System.out.println("****After Sorting****"); list.stream().forEach(name -> System.out.println(name)); list.clear(); list = new ArrayList<Long>(); list.add(123L); list.add(34L); list.add(567L); // sorting using stream System.out.println("**** Sorting using list stream ****"); list.stream().sorted().forEach(value -> System.out.println(value)); } }
Output
****Before Sorting**** 123 34 567 ****After Sorting**** 34 123 567 **** Sorting using list stream **** 34 123 567
Double Wrapper Classe Type List Sorting
ListDoubleSorting.java
package com.narayanatutorial.primitive.sorting; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class ListDoubleSorting { public static void main(String args[]) { List<Double> list = new ArrayList<Double>(); list.add(123.5); list.add(34.4); list.add(567.3); System.out.println("****Before Sorting****"); list.stream().forEach(name -> System.out.println(name)); Collections.sort(list); System.out.println("****After Sorting****"); list.stream().forEach(name -> System.out.println(name)); list.clear(); list = new ArrayList<Double>(); list.add(123.5); list.add(34.4); list.add(567.3); // sorting using stream System.out.println("**** Sorting using list stream ****"); list.stream().sorted().forEach(value -> System.out.println(value)); } }
Output
****Before Sorting**** 123.5 34.4 567.3 ****After Sorting**** 34.4 123.5 567.3 **** Sorting using list stream **** 34.4 123.5 567.3
Get full source code from Git.
Hello! I am Narayanaswamy founder and admin of narayanatutorial.com. I have been working in the IT industry for more than 12 years. NarayanaTutorial is my web technologies blog. My specialties are Java / J2EE, Spring, Hibernate, Struts, Webservices, PHP, Oracle, MySQL, SQLServer, Web Hosting, Website Development, and IAM(ForgeRock) Specialist
I am a self-learner and passionate about training and writing. I am always trying my best to share my knowledge through my blog.