EN
Java - sort ArrayList
0 points
In this article, we would like to show you how to sort ArrayList in Java.
Quick solution:
xxxxxxxxxx
1
Collections.sort(arraylist);
In this example, we sort fruits
ArrayList in alphabetical order using Collections.sort()
method.
xxxxxxxxxx
1
import java.util.*;
2
3
public class Example {
4
5
public static void main(String[] args) {
6
List<String> fruits = new ArrayList<>();
7
8
// add items to the ArrayList
9
fruits.add("Banana");
10
fruits.add("Cherry");
11
fruits.add("Apple");
12
13
// sort ArrayList
14
Collections.sort(fruits);
15
16
fruits.forEach(System.out::println);
17
}
18
}
Output:
xxxxxxxxxx
1
Apple
2
Banana
3
Cherry