How to Sort ArrayList of String and Integer in Java
In this tutorial you will learn how to sort ArrayList of String or Integer in Java. We can easily sort an arraylist in ascending or descending order using Collections.sort() method. Below I have shared simple example for each of them. Sort ArratList in Java Example to Sort ArrayList of String Below program sorts an arraylist of String type.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package com; import java.util.ArrayList; import java.util.Collections; public class SortStringArrayList { public static void main(String args[]){ ArrayList<String> aList = new ArrayList<String>(); aList.add("Android"); aList.add("Windows"); aList.add("IOS"); aList.add("Linux"); aList.add("Mac"); System.out.println("Sort in ascending order"); Collections.sort(aList); for(String s:aList){ System.out.println(s); } System.out.println("\nSort in descending order"); Collections.sort(aList,Collections.reverseOrder()); for(String s:aList){ System.out.println(s); } } } |
Output Sort in ascending order Android IOS Linux Mac Windows Sort in descending order Windows Mac Linux IOS Android
Read more