How to Sort an ArrayList in Java


In this example we will show the method to sort an ArrayList in Java.

Source Code

package com.beginner.examples;

import java.util.ArrayList;
import java.util.Collections;

public class SortArrayList {
  public static void main(String[] args) {
    ArrayList strs = new ArrayList();
    strs.add("a");
    strs.add("m");
    strs.add("c");
    Collections.sort(strs);  // sort an ArrayList
    System.out.println(strs);
  }
}

Output:

[a, c, m]

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments