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
🌐
W3Schools
w3schools.com › java › ref_arraylist_sort.asp
Java ArrayList sort() Method
How Tos Add Two Numbers Swap Two Variables Even or Odd Number Reverse a Number Positive or Negative Square Root Area of Rectangle Celsius to Fahrenheit Sum of Digits Check Armstrong Num Random Number Count Words Count Vowels in a String Remove Vowels Count Digits in a String Reverse a String Palindrome Check Check Anagram Convert String to Array Remove Whitespace Count Character Frequency Sum of Array Elements Find Array Average Sort an Array Find Smallest Element Find Largest Element Second Largest Array Min and Max Array Merge Two Arrays Remove Duplicates Find Duplicates Shuffle an Array Factorial of a Number Fibonacci Sequence Find GCD Check Prime Number ArrayList Loop HashMap Loop Loop Through an Enum
Discussions

[Java] How to sort an arraylist of Objects, based on Objects String field?
You mentioned both components that you needed: You need to implement Comparable, which it looks like you've done (although with a strange second line that's kinda redundant) You then just call Collections.sort(myList) where myList is some sort of List Alternatively, you can create a Comparator (this is a separate class you'll have to define, and then instantiate an instance of it) and then call Collections.sort(myList, myComparator) More on reddit.com
🌐 r/learnprogramming
2
1
April 27, 2022
Best Way to Sort a List in Java (Descending Order) - TestMu AI Community
What is the best way to sort a List/ArrayList in Java? I have a List of doubles and I need to sort it in descending order. I am using an ArrayList and my input data looks like this: List testList = new ArrayLis… More on community.testmu.ai
🌐 community.testmu.ai
0
February 9, 2025
java - Sort objects in ArrayList by date? - Stack Overflow
Every example I find is about doing this alphabetically, while I need my elements sorted by date. My ArrayList contains objects on which one of the datamembers is a DateTime object. On DateTime I ... More on stackoverflow.com
🌐 stackoverflow.com
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
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-sort-an-arraylist
Java Program to Sort an ArrayList - GeeksforGeeks
July 23, 2025 - This sort() Method accepts the list object as a parameter and it will return an ArrayList sorted in ascending order. The syntax for the sort() method is like below. ... All elements in the ArrayList must be mutually comparable, else it throws ...
🌐
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.
🌐
Reddit
reddit.com › r/learnprogramming › [java] how to sort an arraylist of objects, based on objects string field?
r/learnprogramming on Reddit: [Java] How to sort an arraylist of Objects, based on Objects String field?
April 27, 2022 -

How do i alphabetically sort an arraylist of objects, based on the objects field "name". I have object Car, with a compareTo method:

  @Override
  public int compareTo(Car cName) {
 
    int last = this.carName.compareTo(cName.carName);

    return last == 0 ? this.carName.compareTo(cName.carName) : last;

  }

In a separate class there is an array list that captures all of these Car Objects. I need to sort that list alphabetically, then print out the table of Car Objects in alphabetical order. Im stuck trying to implement the above into the sort for the arraylist that i will iteratively print over.

I have looked at Collections.sort() but i don't want to edit the class name to implement comparator.

 public void printRacers() {
    // get
    //
    ArrayList<Car> carNames = new ArrayList<>();
    carNames.addAll(racers);
    
    System.out.println("Car name    Race    Car  number");
    for (int i = 0; i < carNames.size(); i++) {
      Car c = carNames.get(i);
      
      String cName = c.getCarName();
      String cRace = d.getRaceName();
      int sNum = c.getCarNumber();

      String outputString = cName + "   " + cClass + "   " + sNum;
      System.out.println(outputString);

    }

Im really stuck on this and would appreciate anyone's help!

Thanks

🌐
Vultr
docs.vultr.com › java › standard-library › java › util › ArrayList › sort
Java ArrayList sort() - Order Elements | Vultr Docs
November 15, 2024 - The output will display the sorted list: [1, 5, 9]. Understand that Comparator provides a way to define the custom order. Implement a comparator to sort the list in descending order. Use the Collections.sort() with the comparator.
🌐
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.
Find elsewhere
🌐
Programiz
programiz.com › java-programming › library › arraylist › sort
Java ArrayList sort()
Become a certified Java programmer. Try Programiz PRO! ... The sort() method sorts the elements in an arraylist according to the specified order.
🌐
TestMu AI Community
community.testmu.ai › ask a question
Best Way to Sort a List in Java (Descending Order) - TestMu AI Community
February 9, 2025 - What is the best way to sort a List/ArrayList in Java? I have a List of doubles and I need to sort it in descending order. I am using an ArrayList and my input data looks like this: List testList = new ArrayList (); testList.add(0.5); testList.add(0.2); testList.add(0.9); testList.add(0.1); testList.add(0.1); testList.add(0.1); testList.add(0.54); testList.add(0.71); testList.add(0.71); testList.add(0.71); testList.add(0.92); testList.add(0.12); testList.add(0.65); testList.add(0.34)...
🌐
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 an ArrayList is needed ... value. Since ArrayList is part of the Java Collections Framework, we can use the Collections.sort() method....
Top answer
1 of 15
481

You can make your object comparable:

public static class MyObject implements Comparable<MyObject> {

  private Date dateTime;

  public Date getDateTime() {
    return dateTime;
  }

  public void setDateTime(Date datetime) {
    this.dateTime = datetime;
  }

  @Override
  public int compareTo(MyObject o) {
    return getDateTime().compareTo(o.getDateTime());
  }
}

And then you sort it by calling:

Collections.sort(myList);

However sometimes you don't want to change your model, like when you want to sort on several different properties. In that case, you can create comparator on the fly:

Collections.sort(myList, new Comparator<MyObject>() {
  public int compare(MyObject o1, MyObject o2) {
      return o1.getDateTime().compareTo(o2.getDateTime());
  }
});

However, the above works only if you're certain that dateTime is not null at the time of comparison. It's wise to handle null as well to avoid NullPointerExceptions:

public static class MyObject implements Comparable<MyObject> {

  private Date dateTime;

  public Date getDateTime() {
    return dateTime;
  }

  public void setDateTime(Date datetime) {
    this.dateTime = datetime;
  }

  @Override
  public int compareTo(MyObject o) {
    if (getDateTime() == null || o.getDateTime() == null)
      return 0;
    return getDateTime().compareTo(o.getDateTime());
  }
}

Or in the second example:

Collections.sort(myList, new Comparator<MyObject>() {
  public int compare(MyObject o1, MyObject o2) {
      if (o1.getDateTime() == null || o2.getDateTime() == null)
        return 0;
      return o1.getDateTime().compareTo(o2.getDateTime());
  }
});
2 of 15
118

Since Java 8 the List interface provides the sort method. Combined with lambda expression the easiest solution would be

// sort DateTime typed list
list.sort((d1,d2) -> d1.compareTo(d2));
// or an object which has an DateTime attribute
list.sort((o1,o2) -> o1.getDateTime().compareTo(o2.getDateTime()));
// or like mentioned by Tunaki
list.sort(Comparator.comparing(o -> o.getDateTime()));

Reverse sorting

Java 8 comes also with some handy methods for reverse sorting.

//requested by lily
list.sort(Comparator.comparing(o -> o.getDateTime()).reversed());
🌐
Coderanch
coderanch.com › t › 707093 › java › Sorting-ArrayList
Sorting an ArrayList [Solved] (Beginning Java forum at Coderanch)
March 4, 2019 - But before doing anything, read this part of the Java™ Tutorials, and the links therein to Comparable and Comparator. After that you will know that Comparable objects are naturally ordered by a criterion, so I believe LocalDate and LocalTime will both implement Comparable. But an Afspraak doesn't have a criterion you can sort it by, but multiple criteria. In which case I think it is better to create a Comparator<Afspraak>, as Winston has already told you.
🌐
Codecademy
codecademy.com › docs › java › arraylist › .sort()
Java | ArrayList | .sort() | Codecademy
January 5, 2024 - In this example, strings implements the Comparable interface, so they can be sorted directly using Arrays.sort():
🌐
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.
🌐
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.

🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java
Java sort ArrayList Example - Java Code Geeks
May 1, 2019 - Arrays are an essential part of ... they offer. Be it, tuples, lists or vectors, in Python, C# or Java. As coders we will always use these types of collections. This however is an article about sorting an ArrayList in java....
🌐
TutorialsPoint
tutorialspoint.com › sort-elements-in-an-arraylist-in-java
Sort Elements in an ArrayList in Java
June 25, 2020 - In order to sort elements in an ArrayList in Java, we use the Collections.sort() method in Java. This method sorts the elements available in the particular list of the Collection class in ascending order. Declaration −
🌐
Coderanch
coderanch.com › t › 482770 › java › SOLVED-Sorting-ArrayList
[SOLVED] Sorting an ArrayList [Solved] (Beginning Java forum at Coderanch)
February 12, 2010 - Here is my current code. and the error messages I am getting are: I've looked at quite a few explanations of sorting arraylists, and they all achieve sorting with Collections.sort( ArrayList );, but this isn't working for me. They all used string values for sorting and I am using numbers, but ...
🌐
Intellipaat
intellipaat.com › home › blog › java program to sort arraylist of custom objects by property
Java Program to Sort ArrayList of Custom Objects by Property - Intellipaat
1 month ago - In Java, sorting an ArrayList of custom objects can be done by using both the Comparator and Comparable interfaces. The Comparator allows for the custom sorting defined outside the object class, providing flexibility for different sorting criteria.