Published on August 12, 2024By DeveloperBreeze

Java: How to Sort a List

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

public class SortExample {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("Banana");
        list.add("Apple");
        list.add("Orange");

        Collections.sort(list);

        for (String fruit : list) {
            System.out.println(fruit);
        }
    }
}

Comments

Please log in to leave a comment.

Continue Reading:

Sort a List

Published on January 26, 2024

python

Remove Duplicates from a List

Published on January 26, 2024

python

Calculate Average of Numbers

Published on January 26, 2024

python

Enumerating List Elements

Published on January 26, 2024

python

Find Maximum Value in a List

Published on January 26, 2024

python