Sorting ArrayList in Ascending and Descending Order


import java.util .*;

public class Test {
public static void main(String[] args) {
List<Integer> listofYears = new ArrayList<Integer>();
listofYears.add(2021);
listofYears.add(2019);
listofYears.add(2018);
listofYears.add(2020);
// print the ArrayList before sorting
System.out.println("ArrayList before sorting");
System.out.println(listofYears);
// sorting an ArrayList of Integer in ascending order
Collections.sort(listofYears);
// print the ArrayList after sorting
System.out.println("ArrayList after sorting in Ascending");
System.out.println(listofYears);
// sorting an ArrayList of Integer in descending order
Collections.sort(listofYears, Collections.reverseOrder());
// print the ArrayList after sorting
System.out.println("ArrayList after sorting in Descending");
System.out.println(listofYears);
}
}

Comments

Popular posts from this blog

Android - Using KeyStore to encrypt and decrypt the data

Stack and Queue

Java Reflection API