[Java] Comparator of Collection class

Comparator… A comparison function that performs global ordering in a collection

I want to sort the elements of List in ascending order

		public static void main(String[] args) {
		    List<String> list = Arrays.asList("apple","melon","banana","cherry","orange");
		    Collections.sort(list, Comparator.naturalOrder());
		    System.out.println(list);
		  }

I want to sort the elements of List in descending order

		public static void main(String[] args) {
		    List<String> list = Arrays.asList("apple","melon","banana","cherry","orange");
		    Collections.sort(list, Comparator.reverseOrder());
		    System.out.println(list);
		  }

Recommended Posts

[Java] Comparator of Collection class
Expired collection of java
Summary of Java Math class
Java9 collection
[Java] Set structure of collection class (about HashSet and TreeSet)
Java class methods
Significance of interface learned from Java Collection
[Java] Class inheritance
java Scanner class
Java HashMap class
java (abstract class)
[Java] Inheritance and structure of HttpServlet class
Java Reintroduction-Java Collection
[Java] Nested class
Java anonymous class
Mechanism and characteristics of Collection implementation class often used in Java
About Java class
[java] abstract class
[Java] Object class
Java local class
[Java] Overview of Java
[Java] Contents of Collection interface and List interface
[Java] Collection framework
A collection of simple questions for Java beginners
A quick review of Java learned in class
First touch of the Files class (or Java 8)
Use of Date class
Predicted Features of Java
[Java] Significance of serialVersionUID
Java collection interview questions
About Java StringBuilder class
NIO.2 review of java
Review of java Shilber
[Java] About Singleton Class
java --Unification of comments
Java inner class review
Java class type field
History of Java annotation
java (merits of polymorphism)
Java programming (class method)
About Java String class
NIO review of java
Java programming (class structure)
[Java] Three features of Java
Summary of Java support 2018
About java abstract class
[Java] Sort ArrayList with elements of your own class
[Java] How to use compareTo method of Date class
A quick review of Java learned in class part4
Browse class objects in Kotlin (instead of Java class name.class)
A quick review of Java learned in class part3
A quick review of Java learned in class part2
Use of Abstract Class and Interface properly in Java
Java: Use Stream to sort the contents of the collection
[Java] Object operation of ArrayList class (AOJ â‘£ Inversion of sequence)
[Java] Handling of character strings (String class and StringBuilder class)
Summarize the additional elements of the Optional class in Java 9
[Java] Integer wrapper class reference
Getting Started with Java Collection
About an instance of java
[Java] Mirage-Basic usage of SQL