Use a custom comparator:

Collections.sort(nodeList, new Comparator<DataNode>(){
     public int compare(DataNode o1, DataNode o2){
         if(o1.degree == o2.degree)
             return 0;
         return o1.degree < o2.degree ? -1 : 1;
     }
});
Answer from Mark Elliot on Stack Overflow
🌐
Java67
java67.com › 2017 › 07 › how-to-sort-arraylist-of-objects-using.html
How to Sort ArrayList of Objects by Fields in Java? Custom + Reversed Order Sorting Comparator Example | Java67
You can further see these Java 8 Tutorials and courses to learn more about new methods added on the Comparator and List interface to make sorting easier in Java 8. In this article, I'll show you how to sort a List of objects in both ascending and descending order by using Comparator.
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
java - How to sort List by field in descending order? - Stack Overflow
I am trying to achieve a sort for object's field value stored in List. I found the following solution to compare the string but How I can compare the String value and sort accordingly? I am looking... More on stackoverflow.com
🌐 stackoverflow.com
Sorting an ArrayList of objects by their attribute of type double in Java/OOP
I tried to use Arrays.sort for the first time but I'm not really getting anywhere. What does "not really getting anywhere" mean? What specifically did you try, and what didn't work? You'll need to create a custom Comparator object and pass it to sort in order to define what order the values should be sorted in. More on reddit.com
🌐 r/learnprogramming
6
4
October 9, 2022
Sort Java ArrayList based on an object's attribute​
I may be understanding the question wrong, but if you are using Collections.sort() just make a new Comparator. More on reddit.com
🌐 r/learnprogramming
2
5
December 29, 2018
🌐
BezKoder
bezkoder.com › home › java – sort arraylist of objects
Java - Sort ArrayList of Objects - BezKoder
December 27, 2019 - Then we can get a new ArrayList sorted by field for descending order with additional Comparator.reversed() method.
🌐
BeginnersBook
beginnersbook.com › 2013 › 12 › java-arraylist-of-object-sort-example-comparable-and-comparator
Java ArrayList of Object Sort Example (Comparable And Comparator)
Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java · In this tutorial, you will learn how to sort an ArrayList of Objects by property using comparable and comparator interface. If you are looking for sorting a simple ArrayList of String or Integer then you can refer the following tutorials – · Sorting of ArrayList<String> and ArrayList<Integer> Sorting of ArrayList in descending order ·
🌐
How to do in Java
howtodoinjava.com › home › java sorting › java collections sort()
Java Collections sort() - HowToDoInJava
December 14, 2022 - Learn to sort a collection of objects by field either in default ordering using Comparable or custom sorting using Comparator interfaces.
🌐
Blogger
javarevisited.blogspot.com › 2023 › 10 › 6-ways-to-sort-arraylist-in-java.html
6 ways to sort ArrayList in Java 8? List.sort() + Collections.sort() Example Tutorial
October 4, 2023 - Earlier, the order was random, actually the insertion order. In order to sort an ArrayList or List in descending order, we just need to provide a Comparator which reverse the order of original 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

Find elsewhere
🌐
Amir Boroumand
steelcityamir.com › blog › sort-list-of-objects-by-field-java
Sort a List of Objects by Field in Java · Amir Boroumand | Software engineer based in Pittsburgh, PA
May 9, 2018 - This option requires modifying our class and defining a default (or natural) sort order. Sorting in descending order requires two separate method calls. This option doesn’t allow for sorting on a different field other than the one we chose.
🌐
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 - This class will override the compare method of the Comparator class which accepts two objects of the Student class as parameters and returns the comparison value according to our requirement whether we want to sort the array in ascending or descending order and on which attribute, we want to sort the list. ... // Java Program to Sort ArrayList of Custom Object // Using Comparator class // Importing required classes import java.util.*; import java.util.Comparator; // Class public class Student { // Class data members private String studentname; private int rollno; private int studentage; // Con
🌐
Medium
medium.com › @AlexanderObregon › sorting-lists-in-java-using-collections-sort-14b150ac8682
Sorting Lists in Java Using Collections.sort | Medium
June 4, 2025 - Learn how Java’s Collections.sort works behind the scenes, including ascending and descending order, comparator logic, null handling, and TimSort mechanics.
🌐
Vultr
docs.vultr.com › java › standard library › java › util › arraylist › sort()
Java ArrayList sort() - Order Elements
November 15, 2024 - Here, Comparator.reverseOrder() is used to sort the list in descending order. The result will show [9, 5, 1]. Define a class with multiple fields. Create a comparator that defines a custom sort order based on one of the fields.
🌐
Medium
medium.com › @er.pankajsonagara › java-how-do-you-sort-an-arraylist-in-descending-order-57b3a5c2935e
Java: How do you sort an ArrayList in descending order? | by Pankaj Sonagara | Medium
April 9, 2024 - import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; public class ArrayListSort2 { public static void main(String[] args) { List list = new ArrayList<>(Arrays .asList(new Student[] { new Student(1, "Hardik"), new Student(2, "Rohit"), new Student(3, "Aaryan") })); System.out.println("Sort by ascending order."); Collections.sort(list,new ConpareByName(ShortBy.ASC)); System.out.println(list); System.out.println("Sort by descending order."); Collections.sort(list,new ConpareByName(ShortBy.DES)); System.out.println(li
🌐
Scaler
scaler.com › home › topics › how to sort arraylist in java?
How to Sort ArrayList in Java? - Scaler Topics
October 14, 2022 - Inside the method, we have passed the Comparator.reverseOrder(). Here the reverseOrder() method of the Comparator interface sort the elements of the ArrayList in reverse order that is, in descending order. Throughout the article, the comparator term is mentioned many times, let us now understand ...
🌐
Javatpoint
javatpoint.com › how-to-sort-arraylist-in-java
How to Sort ArrayList in Java - javatpoint
Sorting a Java Vector in Descending Order Using Comparator · Types of Interfaces in Java · Understanding String Comparison Operator in Java · User-Defined Packages in Java · Valid variants of main() in Java · What is a Reference Variable in Java · What is an Instance in Java · What is Retrieval Operation in ArrayList Java ·
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-sort-an-arraylist-in-descending-order-in-java
How to sort an ArrayList in Descending Order in Java - GeeksforGeeks
December 11, 2018 - // Java program to demonstrate ...tln("Unsorted ArrayList: " + list); // Sorting ArrayList in descending Order // using Collection.sort() method // by passing Collections.reverseOrder() as comparator Collections.sort(list, ...
🌐
Blogger
javarevisited.blogspot.com › 2012 › 01 › how-to-sort-arraylist-in-java-example.html
How to sort ArrayList in Natural and Custom Order in Java - Example Tutorial
By using List.of() method, by using Collections.sort() method or by using Stream.sort() in Java 8? Let me know in comments. ... Anonymous said... Should have mention that Object should be implemented Comparable interface in order to them to be sorted in Arraylist in natural order. first natural order is applied and than ascending or descending which determine direction.
🌐
BeginnersBook
beginnersbook.com › 2013 › 12 › sort-arraylist-in-descending-order-in-java
How to sort ArrayList in descending order in Java
September 11, 2022 - In the above example we have sorted the ArrayList of String type (ArrayList<String>), the logic is same for other type of ArrayList as well. import java.util.*; public class JavaExample { public static void main(String args[]){ ArrayList<String> arrList = new ArrayList<String>(); arrList.add("Chaitanya"); arrList.add("BeginnersBook"); arrList.add("Tutorials"); arrList.add("Website"); /*Unsorted List: ArrayList content before sorting*/ System.out.println("Before Sorting:"); for(String str: arrList){ System.out.println(str); } // Sorting in decreasing order Collections.sort(arrList); //sorting in ascending order Collections.reverse(arrList); //reversing the sorted list // print sorted arraylist System.out.println();//new line System.out.println("After sorting: "); for(String str: arrList){ System.out.println(str); } } }
🌐
DZone
dzone.com › data engineering › data › different approaches to sorting elements of an arraylist in java
Java ArrayList: Approaches to Sorting Elements
November 1, 2015 - Sorting ArrayList using Comparable is a common approach. But, you need to be aware of certain constraints. The class whose object you want to sort must implement Comparable and override the compareTo() method. This essentially means you would only be able to compare the objects based on one field (which was age in our example).
🌐
W3Schools
w3schools.com › java › ref_arraylist_sort.asp
Java ArrayList sort() Method
The sort() method sorts items in the list. A Comparator can be used to compare pairs of elements.