Collections.sort(testList);
Collections.reverse(testList);

That will do what you want. Remember to import Collections though!

Here is the documentation for Collections.

Answer from tckmn on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › ArrayList.html
ArrayList (Java Platform SE 8 )
October 20, 2025 - Sorts this list according to the order induced by the specified Comparator.
Discussions

How can I sort an arrayList containing objects without using a sort method from the java collections library?
https://en.wikipedia.org/wiki/Selection_sort#Implementations Translate that to Java. More on reddit.com
🌐 r/learnprogramming
21
2
June 25, 2022
java - Sort ArrayList of custom Objects by property - Stack Overflow
I read about sorting ArrayLists using a Comparator but in all of the examples people used compareTo which according to some research is a method for Strings. I wanted to sort an ArrayList of custom More on stackoverflow.com
🌐 stackoverflow.com
Add std.ArrayList.sort
There was an error while loading. Please reload this page · Add a ArrayList.sort method that wraps std.sort More on github.com
🌐 github.com
5
April 10, 2019
java getList method / arrayList sort
Posted by u/Agent__Johnson - No votes and no comments More on reddit.com
🌐 r/learnprogramming
0
July 16, 2016
🌐
W3Schools
w3schools.com › java › ref_arraylist_sort.asp
Java ArrayList sort() Method
Non-primitive types must implement Java's Comparable interface in order to be sorted without a comparator. ... import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> cars = new ArrayList<String>(); cars.add("Volvo"); cars.add("BMW"); cars.add("Ford"); cars.add("Mazda"); cars.sort( (a, b) -> { return -1 * a.compareTo(b); } ); System.out.println(cars); } }
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › system.collections.arraylist.sort
ArrayList.Sort Method (System.Collections) | Microsoft Learn
Sorts the elements in a range of elements in ArrayList using the specified comparer. public: virtual void Sort(int index, int count, System::Collections::IComparer ^ comparer);
🌐
Codecademy
codecademy.com › docs › java › arraylist › .sort()
Java | ArrayList | .sort() | Codecademy
January 5, 2024 - In this example, the Arrays.sort(numbers) method is used to sort the array of integers in ascending order.
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-sort-an-arraylist-in-ascending-order-in-java
How to sort an ArrayList in Ascending Order in Java - GeeksforGeeks
January 22, 2026 - In this article, we will learn how to sort an ArrayList in ascending order in Java using built-in utilities.
Find elsewhere
🌐
BeginnersBook
beginnersbook.com › 2013 › 12 › java-arraylist-of-object-sort-example-comparable-and-comparator
Java ArrayList of Object Sort Example (Comparable And Comparator)
We generally use Collections.sort() method to sort a simple array list. However if the ArrayList is of custom object type then in such case you have two options for sorting- comparable and comparator interfaces.
🌐
Programiz
programiz.com › java-programming › library › arraylist › sort
Java ArrayList sort()
The sort() method sorts the elements in an arraylist according to the specified order.
🌐
Reddit
reddit.com › r/learnprogramming › how can i sort an arraylist containing objects without using a sort method from the java collections library?
r/learnprogramming on Reddit: How can I sort an arrayList containing objects without using a sort method from the java collections library?
June 25, 2022 -

I have this code I am working on for a homework assignment, but the professor has specified that we cannot use anything from java collections. The issue is that all of his examples use collections, and all of his examples about sorting only cover sorting an int array. The code I am writing has to sort the array by both "rollno"(the int variable assigned to each "student" object) and by "name"(the string variable assigned to each "student").

The assignment calls for the use of a selection sort method, so I want to sort the numbers in order from 1-10 and sort the names by string length. If anyone can guide me forward on this I would greatly appreciate it because I am lost on how to even begin a selection sort method.

Sidenote: The github has four files in it; "arrSort", "Student", "rollComp", and "nameComp". "arrSort" is the main method and is where I am trying to implement the sort method, "Student" is the object class, "rollComp" and "nameComp" are both comparator classes. All of these files are copied directly from my IDE, and if you notice ANY errors in how I have formatted anything, or if you have any other questions please let me know.

🌐
Coderanch
coderanch.com › t › 707093 › java › Sorting-ArrayList
Sorting an ArrayList [Solved] (Beginning Java forum at Coderanch)
March 4, 2019 - Juan Bauer wrote:Can someone point me in the good direction? Yes. Create a Comparator to sort your objects in whatever way you want, and then use Collections.sort(Comparator). Hope it helps.
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-sort-an-arraylist-of-objects-by-property-in-java
How to Sort an ArrayList of Objects by Property in Java? - GeeksforGeeks
July 23, 2025 - // Java Program to Sort ArrayList of Objects by Property // Importing required classes import java.util.*; // Class 1 // Custom class with custom property // It takes and stores custom objects class CustomObject { // Class data member private String customProperty; // Method public CustomObject(String property) { this.customProperty = property; } // Getter public String getCustomProperty() { return this.customProperty; } } // Class 2 public class GFG { // Method 1 // To print sorted ArrayList objects // using enhanced for loop public static void print(ArrayList<CustomObject> list) { for (Custo
🌐
Vultr
docs.vultr.com › java › standard-library › java › util › ArrayList › sort
Java ArrayList sort() - Order Elements | Vultr Docs
November 15, 2024 - The sort() method in Java's ArrayList class is a versatile tool for organizing elements in a specified order, whether ascending, descending, or by a custom-defined criteria.
🌐
Scaler
scaler.com › home › topics › how to sort arraylist in java?
How to Sort ArrayList in Java? - Scaler Topics
October 14, 2022 - Basically, it takes the elements of ArrayList as parameters and sorts them in ascending order, and returns an ArrayList that is sorted in ascending order, with respect to the original ordering of its elements.
🌐
Spring Framework Guru
springframework.guru › home › sorting arraylists in java: a practical guide
Sorting ArrayLists in Java: A Practical Guide - Spring Framework Guru
October 22, 2024 - Sorting ArrayList objects is a common task that can be accomplished in several ways. We can sort ArrayLists using the simple Collections.sort() method, or more advanced techniques using custom comparators, lambdas, and the Stream API.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-sort-an-arraylist
Java Program to Sort an ArrayList - GeeksforGeeks
July 23, 2025 - An ArrayList of Wrapper class object is nothing but an ArrayList of objects like String, Integers, etc. An ArrayList can be sorted in two ways ascending and descending order. The collection class provides two methods for sorting ArrayList.
Top answer
1 of 16
1736

Since Date implements Comparable, it has a compareTo method just like String does.

So your custom Comparator could look like this:

public class CustomComparator implements Comparator<MyObject> {
    @Override
    public int compare(MyObject o1, MyObject o2) {
        return o1.getStartDate().compareTo(o2.getStartDate());
    }
}

The compare() method must return an int, so you couldn't directly return a boolean like you were planning to anyway.

Your sorting code would be just about like you wrote:

Collections.sort(Database.arrayList, new CustomComparator());

A slightly shorter way to write all this, if you don't need to reuse your comparator, is to write it as an inline anonymous class:

Collections.sort(Database.arrayList, new Comparator<MyObject>() {
    @Override
    public int compare(MyObject o1, MyObject o2) {
        return o1.getStartDate().compareTo(o2.getStartDate());
    }
});

Since java-8

You can now write the last example in a shorter form by using a lambda expression for the Comparator:

Collections.sort(Database.arrayList, 
                        (o1, o2) -> o1.getStartDate().compareTo(o2.getStartDate()));

And List has a sort(Comparator) method, so you can shorten this even further:

Database.arrayList.sort((o1, o2) -> o1.getStartDate().compareTo(o2.getStartDate()));

This is such a common idiom that there's a built-in method to generate a Comparator for a class with a Comparable key:

Database.arrayList.sort(Comparator.comparing(MyObject::getStartDate));

All of these are equivalent forms.

2 of 16
202

Classes that has a natural sort order (a class Number, as an example) should implement the Comparable interface, whilst classes that has no natural sort order (a class Chair, as an example) should be provided with a Comparator (or an anonymous Comparator class).

Two examples:

public class Number implements Comparable<Number> {
    private int value;

    public Number(int value) { this.value = value; }
    public int compareTo(Number anotherInstance) {
        return this.value - anotherInstance.value;
    }
}

public class Chair {
    private int weight;
    private int height;

    public Chair(int weight, int height) {
        this.weight = weight;
        this.height = height;
    }
    /* Omitting getters and setters */
}
class ChairWeightComparator implements Comparator<Chair> {
    public int compare(Chair chair1, Chair chair2) {
        return chair1.getWeight() - chair2.getWeight();
    }
}
class ChairHeightComparator implements Comparator<Chair> {
    public int compare(Chair chair1, Chair chair2) {
        return chair1.getHeight() - chair2.getHeight();
    }
}

Usage:

List<Number> numbers = new ArrayList<Number>();
...
Collections.sort(numbers);

List<Chair> chairs = new ArrayList<Chair>();
// Sort by weight:
Collections.sort(chairs, new ChairWeightComparator());
// Sort by height:
Collections.sort(chairs, new ChairHeightComparator());

// You can also create anonymous comparators;
// Sort by color:
Collections.sort(chairs, new Comparator<Chair>() {
    public int compare(Chair chair1, Chair chair2) {
        ...
    }
});
🌐
YouTube
youtube.com › daniel sutantyo
CP-1.010 Java Tutorial - Sorting an ArrayList - YouTube
Problem: Sort of Sorting (https://open.kattis.com/problems/sortofsorting)In this video I discuss how you can sort an ArrayList. In case you haven't used an A...
Published   September 14, 2021
Views   3K
🌐
How to do in Java
howtodoinjava.com › home › collections framework › java arraylist › java arraylist sort: ascending and descending order
Java ArrayList Sort: Ascending and Descending Order
August 4, 2023 - For specific cases, it is good to know the requirements and use the customized solution: Implement Comparable interface for natural ordering, and use Comparator instances for custom and reverse ordering · Use List.sort() if we want to modify ...
🌐
GitHub
github.com › ziglang › zig › issues › 2246
Add std.ArrayList.sort · Issue #2246 · ziglang/zig
April 10, 2019 - Add std.ArrayList.sort#2246 · Copy link · Labels · proposalThis issue suggests modifications. If it also has the "accepted" label then it is planned.This issue suggests modifications. If it also has the "accepted" label then it is planned.standard libraryThis issue involves writing Zig code for the standard library.This issue involves writing Zig code for the standard library.
Published   Apr 10, 2019
🌐
BeginnersBook
beginnersbook.com › 2013 › 12 › how-to-sort-arraylist-in-java
How to sort ArrayList in Java
We can use Collections.sort() method to sort an ArrayList in ascending and descending order.