Use this method Collections.sort(List,Comparator) . Implement a Comparator and pass it to Collections.sort().

class RecipeCompare implements Comparator<Recipe> {

    @Override
    public int compare(Recipe o1, Recipe o2) {
        // write comparison logic here like below , it's just a sample
        return o1.getID().compareTo(o2.getID());
    }
}

Then use the Comparator as

Collections.sort(recipes,new RecipeCompare());
Answer from AllTooSir on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Collections.html
Collections (Java Platform SE 8 )
July 21, 2026 - These algorithms may, but are not required to, throw this exception if an invocation would have no effect on the collection. For example, invoking the sort method on an unmodifiable list that is already sorted may or may not throw UnsupportedOperationException. This class is a member of the Java Collections Framework.
🌐
GeeksforGeeks
geeksforgeeks.org › java › collections-sort-java-examples
Collections.sort() in Java with Examples - GeeksforGeeks
Collections.sort(List<T> list); Sorts the list in ascending order (natural order) Elements must implement Comparable · Example: Sorting an ArrayList in ascending order · JAVA ·
Published: April 30, 2026
Discussions

Collections.sort()
Ah ha! Your hunch about unicode was in the right direction. Try loading up your text file in a hex editor. I just downloaded the file and loaded it with HxD, and this is the first line, in hex: EF BB BF 41 61 64 61 The 41 61 64 61 is "Aada", and the EF BB BF is referenced in Wikipedia as a UTF-8 byte order marker. So the solution is probably to edit the file to remove those first 3 bytes. https://en.wikipedia.org/wiki/Byte_order_mark#UTF-8 The UTF-8 representation of the BOM is the byte sequence 0xEF,0xBB,0xBF. A text editor or web browser interpreting the text as ISO-8859-1 or CP1252 will display the characters  for this. The Unicode Standard permits the BOM in UTF-8,[2] but does not require or recommend its use.[3] Byte order has no meaning in UTF-8,[4] so its only use in UTF-8 is to signal at the start that the text stream is encoded in UTF-8. Edit: iuhoosierkyle up above also caught this. More on reddit.com
🌐 r/java
14
7
June 23, 2014
How does Collections.sort() organize files?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/javahelp
8
1
September 12, 2022
🌐
W3Schools
w3schools.com › java › java_sort_list.asp
Java Sort a List - List Sorting
Another useful class in the java.util package is the Collections class, which include the sort() method for sorting lists alphabetically or numerically.
🌐
Spring Framework Guru
springframework.guru › home › how to sort java collections
How To Sort Java Collections - Spring Framework Guru
October 22, 2024 - To help us sort objects, Collections.sort() can utilize a Comparator to allow us to specify custom sort criteria. You can see for the output, we are using Java’s versatile printf() method.
🌐
Medium
medium.com › @AlexanderObregon › sorting-lists-in-java-using-collections-sort-14b150ac8682
Sorting Lists in Java Using Collections.sort | Medium
June 4, 2025 - Java provides several tools for sorting, but one of the most commonly used is Collections.sort(). It works with lists of numbers, strings, or even custom objects when you provide it with a way to compare them.
Find elsewhere
🌐
YouTube
youtube.com › watch
Sorting Collections in Java - Mastering the Collections.sort() Method for Efficient Data Sorting - YouTube
🌟 Sorting Collections in Java - Mastering the Collections.sort() Method for Efficient Data SortingWelcome to our Java programming series! In this tutorial, ...
Published: February 24, 2024
🌐
Baeldung
baeldung.com › home › java › java collections › sorting in java
Sorting in Java | Baeldung
February 3, 2026 - Note that we can use Collections.sort() in a similar fashion to sort List and Set of Objects in Natural or Custom order as described above for Arrays. Start with Java 8, we can use Lambdas to implement the Comparator Functional Interface.
🌐
Baeldung
baeldung.com › home › java › java collections › comparing long values using collections.sort()
Comparing Long Values Using Collections.sort() | Baeldung
June 16, 2025 - The Collections.sort() method sorts elements in their natural order by default. For numerical types like Long, natural ordering arranges values from smallest to largest.
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-collections-sort
Java Collections sort() | DigitalOcean
Technical tutorials, Q&A, events — This is an inclusive place where developers can find or lend support and discover new ways to contribute to the community.
🌐
TutorialsPoint
tutorialspoint.com › java › util › collections_sort_comparable.htm
Java Collections sort() Method
The Java Collections sort(List<T>) method is used to sort the specified list into ascending order, according to the natural ordering of its element.
🌐
Medium
medium.com › @reetesh043 › collection-sort-vs-arrays-sort-in-java-1528f83223c3
Medium
February 21, 2024 - This method ensures stability in sorting, meaning that equal elements maintain their relative order after sorting, making it a reliable choice for various sorting tasks in Java applications. Under the hood, Collection.sort() uses a modified version of the merge sort algorithm.
🌐
CodeGym
codegym.cc › java blog › java collections › java collections sort()
Java Collections sort()
October 11, 2023 - Thankfully, Collections.sort()is overwritten with an optional 2nd parameter, which allows you to do just this: sort(List l, Comparator c) But what’s a comparator? Well, a comparator is simply a function that compares two inputs and returns a number representing which input comes first. If you are sorting an ArrayList of primitive data types, then Java Collections already provides you with a reverseOrder() comparator.
🌐
Codecademy
codecademy.com › docs › java › collections › .sort()
Java | Collections | .sort() | Codecademy
July 18, 2022 - import java.util.*; // This sorts by natural ordering Collections.sort(myList); // This sorts by a Comparator Collections.sort(myList, myComparator);
🌐
Reddit
reddit.com › r/java › collections.sort()
r/java on Reddit: Collections.sort()
June 23, 2014 -

So I'm having a strange problem that I cant seem to understand how to fix.

I have written a program that reads names from a txt into an arraylist, and then I use Collections.sort() to put it in alphabetical order,

However, when I run it, Collections.sort() puts what should be the very first name last.

At first I thought, maybe there something weird about the unicode behind that name, so I removed it from the file, but then It did the same thing with the next name. Please help because I want to add a Binary search to this program. this is the file : https://drive.google.com/file/d/0B_ji2Hqu1ouAdDhHemFMal9iU2c/edit?usp=sharing

here is my code:

public static ArrayList<String> feNames = new   ArrayList<String>();

public static void main(String[] args) throws IOException {

    Scanner s = new Scanner(new File("C:\\Users\\Austin\\Desktop\\female names\\female names.txt"));

    while (s.hasNext()) {
        feNames.add(s.next());
    }
    s.close();

    Collections.sort(feNames);
   
     for (String nam : feNames) {
          
          System.out.println(nam);
    }

Thanks for the help!!

Top answer
1 of 5
5
Ah ha! Your hunch about unicode was in the right direction. Try loading up your text file in a hex editor. I just downloaded the file and loaded it with HxD, and this is the first line, in hex: EF BB BF 41 61 64 61 The 41 61 64 61 is "Aada", and the EF BB BF is referenced in Wikipedia as a UTF-8 byte order marker. So the solution is probably to edit the file to remove those first 3 bytes. https://en.wikipedia.org/wiki/Byte_order_mark#UTF-8 The UTF-8 representation of the BOM is the byte sequence 0xEF,0xBB,0xBF. A text editor or web browser interpreting the text as ISO-8859-1 or CP1252 will display the characters  for this. The Unicode Standard permits the BOM in UTF-8,[2] but does not require or recommend its use.[3] Byte order has no meaning in UTF-8,[4] so its only use in UTF-8 is to signal at the start that the text stream is encoded in UTF-8. Edit: iuhoosierkyle up above also caught this.
2 of 5
5
The first character is the problem. To get the file I have used the following code List femaleNames = Files.readAllLines(Paths.get("path_to_file"),Charset.forName("UTF-8")); And then I have printed the first result System.out.println(femaleNames.get(0)); Which gives me ?Aada, which looks wrong. A quick look on Stackoverflow gave me the following answer. . Which is exactly what you're looking for. Edit: Well Stackoverflow just went down for maintenance. But to help you now here is the solution: BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("path_to_file"), "UTF-8")); // skip first character if its the BOM (https://en.wikipedia.org/wiki/Byte_order_mark#UTF-8) in.mark(1); if (in.read() != 0xFEFF) in.reset(); List femaleNames = new ArrayList<>(); String line = new String(); while((line = in.readLine()) != null){ femaleNames.add(line); }
🌐
Substack
javabulletin.substack.com › engineering with java › top 5 tips for sorting java collections
Top 5 Tips For Sorting Java Collections - by Suraj Mishra
December 21, 2025 - Modern Java’s sorting capabilities go far beyond the basic Collections.sort() - they offer elegant, readable, and performant solutions for even the most complex sorting requirements.
🌐
Interview Kickstart
interviewkickstart.com › home › blogs › learn › collections sort in java
Collections Sort in Java | Interview Kickstart
September 25, 2024 - Collections sort is a method of Java Collections class used to sort a list, which implements the List interface. All the elements in the list must be mutually comparable. If a list consists of string elements, then it will be sorted in alphabetical ...
🌐
freeCodeCamp
freecodecamp.org › news › how-to-sort-a-list-in-java
How to Sort a List in Java – Java List Sorting Example
January 24, 2023 - One of the most common ways to sort data in Java is to use the Collections.sort() method.