The Collections.sort() method sorts the list in the natural order. This is the simplest way to sort a list of comparable elements.
import java.util.*;
class Example {
public static void main(String args[]) {
List list = new ArrayList();
list.add("Banana");
list.add("Apple");
list.add("Cherry");
Collections.sort(list);
System.out.println(list);
}
}
Console Output:
[Apple, Banana, Cherry]
A Comparator can be used for custom sorting logic. For example, sorting strings by length.
import java.util.*;
class Example {
public static void main(String args[]) {
List list = new ArrayList();
list.add("Banana");
list.add("Apple");
list.add("Cherry");
Collections.sort(list, new Comparator() {
public int compare(String s1, String s2) {
return s1.length() - s2.length();
}
});
System.out.println(list);
}
}
Console Output:
[Apple, Banana, Cherry]
Using Collections.sort() to sort integers in ascending order.
import java.util.*;
class Example {
public static void main(String args[]) {
List list = new ArrayList();
list.add(3);
list.add(1);
list.add(2);
Collections.sort(list);
System.out.println(list);
}
}
Console Output:
[1, 2, 3]
Lambda expressions can simplify the syntax when implementing a Comparator for custom sorting.
import java.util.*;
class Example {
public static void main(String args[]) {
List list = new ArrayList();
list.add("Banana");
list.add("Apple");
list.add("Cherry");
Collections.sort(list, (s1, s2) -> s2.compareTo(s1));
System.out.println(list);
}
}
Console Output:
[Cherry, Banana, Apple]
Use Comparator to sort a list of custom objects by a specific property.
import java.util.*;
class Person {
String name;
int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
public String toString() {
return name + ":" + age;
}
}
class Example {
public static void main(String args[]) {
List list = new ArrayList();
list.add(new Person("Alice", 30));
list.add(new Person("Bob", 25));
list.add(new Person("Charlie", 35));
Collections.sort(list, (p1, p2) -> p1.age - p2.age);
System.out.println(list);
}
}
Console Output:
[Bob:25, Alice:30, Charlie:35]
The Collections.reverseOrder() method can be used to sort a list in reverse order.
import java.util.*;
class Example {
public static void main(String args[]) {
List list = new ArrayList();
list.add(3);
list.add(1);
list.add(2);
Collections.sort(list, Collections.reverseOrder());
System.out.println(list);
}
}
Console Output:
[3, 2, 1]
Java 8 Stream API provides a sorted() method to sort a list in a functional style.
import java.util.*;
import java.util.stream.*;
class Example {
public static void main(String args[]) {
List list = Arrays.asList("Banana", "Apple", "Cherry");
List sortedList = list.stream().sorted().collect(Collectors.toList());
System.out.println(sortedList);
}
}
Console Output:
[Apple, Banana, Cherry]
Use Comparator.thenComparing() to sort by multiple criteria, such as sorting by name and then by age.
import java.util.*;
class Person {
String name;
int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
public String toString() {
return name + ":" + age;
}
}
class Example {
public static void main(String args[]) {
List list = new ArrayList();
list.add(new Person("Alice", 30));
list.add(new Person("Bob", 25));
list.add(new Person("Alice", 25));
Collections.sort(list, Comparator.comparing(Person::getName).thenComparing(Person::getAge));
System.out.println(list);
}
}
Console Output:
[Alice:25, Alice:30, Bob:25]
Use Comparator.nullsFirst() to handle null values while sorting.
import java.util.*;
class Example {
public static void main(String args[]) {
List list = Arrays.asList("Banana", null, "Cherry", "Apple");
Collections.sort(list, Comparator.nullsFirst(Comparator.naturalOrder()));
System.out.println(list);
}
}
Console Output:
[null, Apple, Banana, Cherry]
Newsletter
Subscribe to our newsletter for weekly updates and promotions.
Wiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesAds Policies