📜  Java ArrayList sort()(1)

📅  最后修改于: 2023-12-03 15:15:55.251000             🧑  作者: Mango

Java ArrayList sort()

Introduction

The sort() method in Java ArrayList is used to sort the elements of an ArrayList in ascending order. It arranges the elements based on their natural ordering or using a custom comparator provided by the programmer.

Syntax

The syntax of the sort() method is as follows:

public void sort()
Example

Consider the following example, which demonstrates the usage of the sort() method:

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

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

        System.out.println("Before sorting: " + fruits);

        // Sorting the ArrayList
        Collections.sort(fruits);

        System.out.println("After sorting: " + fruits);
    }
}

Output:

Before sorting: [Apple, Orange, Banana, Grapes]
After sorting: [Apple, Banana, Grapes, Orange]

In the above example, we create an ArrayList named fruits and add some fruit names to it. We then print the fruits before sorting. After that, we use the Collections.sort() method to sort the fruits ArrayList. Finally, we print the sorted fruits.

Custom Sorting Order

To sort the elements in a custom order, you can use a custom comparator. The comparator provides the logic to compare and sort the elements according to your requirements.

Here's an example:

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

class LengthComparator implements Comparator<String> {
    @Override
    public int compare(String s1, String s2) {
        return Integer.compare(s1.length(), s2.length());
    }
}

public class ArrayListSortCustomOrderExample {
    public static void main(String[] args) {
        ArrayList<String> names = new ArrayList<>();
        names.add("John");
        names.add("Abby");
        names.add("Emma");
        names.add("Adam");
        names.add("Samantha");

        System.out.println("Before sorting: " + names);

        // Sorting the ArrayList using a custom comparator
        Collections.sort(names, new LengthComparator());

        System.out.println("After sorting: " + names);
    }
}

Output:

Before sorting: [John, Abby, Emma, Adam, Samantha]
After sorting: [Abby, John, Emma, Adam, Samantha]

In the above example, we create an ArrayList named names and add some names to it. We then print the names before sorting. After that, we define a custom comparator called LengthComparator that compares the names based on their lengths. We use this comparator in the Collections.sort() method to sort the names ArrayList. Finally, we print the sorted names.

Conclusion

The sort() method in Java ArrayList allows you to easily sort the elements in either their natural order or a custom order defined by a comparator. It is a useful method when you need to arrange the elements in a specific order.