Why not try TreeSet. Does your list not allow duplicates? If so then the Set should be ok. As you are adding strings and this implements Comparator the set will be automatically sorted for you

If you had

Set<String> s = new TreeSet<String>();
s.add("B");
s.add("C");
s.add("A");

then the contents of the set would be A, B, C

Answer from RNJ on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › java list › sort a list alphabetically in java
Sort a List Alphabetically in Java | Baeldung
April 3, 2025 - We can also define our custom rules to sort alphabetically using java text.RuleBasedCollator.
Discussions

java - How can I sort a List alphabetically? - Stack Overflow
The tree set relies on the items being sorted to perform its tasks (like searchting, removing, inserting...). 2011-12-01T12:43:26.357Z+00:00 ... Chai T. Rex · Chai T. Rex Over a year ago · This doesn't answer the question because the default sort used for Strings is lexicographic, not alphabetical... More on stackoverflow.com
🌐 stackoverflow.com
How to sort a list alphabetically and place any items that begin with a number at the bottom
You could write your own comparator that places numbers at the end. Numbers are before letters in Unicode and that's the way the default sorting works. More on reddit.com
🌐 r/javahelp
8
6
April 13, 2020
java - How to sort a HashSet? - Stack Overflow
For lists, we use the Collections.sort(List) method. What if we want to sort a HashSet? More on stackoverflow.com
🌐 stackoverflow.com
April 14, 2015
Custom Alphabetic Sorting of Array in Java - Software Engineering Stack Exchange
I have a requirement to read a text file with lines in tag=value format and then output the file with specific tags listed first and the rest sorted alphabetically. The incoming file is randomly s... More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
May 2, 2013
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-sort-names-in-an-alphabetical-order
Java Program to Sort Names in an Alphabetical Order - GeeksforGeeks
July 23, 2025 - Print the Sorted Names in an Alphabetical Order. Below is the implementation of the above approach: ... // Java Program to Sort Names in an Alphabetical Order import java.io.*; class GFG { public static void main(String[] args) { // storing input in variable int n = 4; // create string array called names String names[] = { "Rahul", "Ajay", "Gourav", "Riya" }; String temp; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { // to compare one string with other strings if (names[i].compareTo(names[j]) > 0) { // swapping temp = names[i]; names[i] = names[j]; names[j] = temp; } } } // print output array System.out.println( "The names in alphabetical order are: "); for (int i = 0; i < n; i++) { System.out.println(names[i]); } } }
🌐
Medium
medium.com › @AlexanderObregon › sorting-text-alphabetically-with-java-code-177cfc072947
Sorting Text Alphabetically with Java Code | Medium
July 26, 2025 - Learn how alphabetical sorting works in Java with Arrays.sort, Collections.sort, and custom comparators. Covers Unicode, case, and locale rules.
🌐
Delft Stack
delftstack.com › home › howto › java › sort a list alphabetically in java
How to Sort a List Alphabetically in Java | Delft Stack
February 12, 2024 - Additionally, TreeSet ensures that elements are arranged in their natural order, providing a straightforward solution for tasks where alphabetical order and uniqueness are essential. This method enhances code simplicity, reducing the need for complex sorting logic and contributing to more efficient and readable Java programs. ... import java.util.Set; import java.util.TreeSet; public class TreeSetSortExample { public static void main(String[] args) { // Creating a TreeSet to store country names Set<String> countries = new TreeSet<>(); countries.add("India"); countries.add("China"); countries.a
Top answer
1 of 16
256

Assuming that those are Strings, use the convenient static method sort:

Collections.sort(listOfCountryNames)
2 of 16
161

Solution with Collections.sort

If you are forced to use this List, or if your program has a structure like

  • Create a list
  • Add some country names
  • sort them once
  • never change that list again

then Thilos answer is the best way to do it. Combine it with the advice from Tom Hawtin - tackline and you get

java.util.Collections.sort(listOfCountryNames, Collator.getInstance());

Solution with a TreeSet

If you have the choice, and if your application is likely to become more complex, you could modify your code to use a TreeSet instead. This kind of collection sorts your items as they are inserted. There is no need to call sort().

Collection<String> countryNames = 
    new TreeSet<String>(Collator.getInstance());
countryNames.add("UK");
countryNames.add("Germany");
countryNames.add("Australia");
// Voila... sorted.

Side note on why I prefer the TreeSet

It has some subtle but important advantages:

  • It's just shorter. But only one line shorter.
  • Never worry about is this list really sorted right now, because a TreeSet is always sorted, no matter what you do.
  • You cannot have duplicate entries. Depending on your situation, this can be a pro or a con. If you need duplicates, stick to your list.
  • An experienced programmer looks at TreeSet<String> countyNames and immediately knows: this is a sorted collection of strings without duplicates, and I can be sure that this is true at any moment. So much information in one short declaration.
  • A real performance gain in some cases. If you use a list, and you insert values very often, and the list may be read between those insertions, then you have to sort the list after each insertion. The set does the same thing, but much faster.

Using the right collection for the right task is a key to writing short and bug-free code. It's not so demonstrative in this case, because you're only saving one line. But I've lost count of how often I see someone using a list when they want to make sure there are no duplicates, and then building that functionality themselves. Or even worse, using two lists when you really need one map.

Don't get me wrong: Using Collections.sort is not a bug or a mistake. But there are many cases where the TreeSet is much cleaner.

🌐
W3Docs
w3docs.com › java
How can I sort a List alphabetically?
To sort a List alphabetically in Java, you can use the Collections.sort method and pass in your List as an argument.
Find elsewhere
🌐
W3Schools
w3schools.com › java › java_sort_list.asp
Java Sort a List - List Sorting
Sort a Java list alphabetically or numerically with Collections.sort().
🌐
Kode Java
kodejava.org › how-do-i-sort-items-in-a-set
How do I sort items in a Set? - Learn Java by Examples
May 18, 2023 - Set<String> set = new TreeSet<>(); // In the example below we add some letters to the TreeSet, this mean // that the alphabets will be ordered based on the alphabet order // which is from A to Z.
🌐
Quora
quora.com › How-do-you-sort-input-alphabetically-in-Java
How to sort input alphabetically in Java - Quora
Answer (1 of 3): I quickly wrote a simple example with a Scanner and sysin. To sort it I used java’s standard lib sort Arrays.sort [code]import java.util.Arrays; import java.util.Scanner; public class Main { public String sortString(String s) { char[] chars = s.toCharArray(); Arrays.sort(...
🌐
BeginnersBook
beginnersbook.com › 2018 › 10 › java-program-to-sort-strings-in-an-alphabetical-order
Java Program to Sort Strings in an Alphabetical Order
Once we have all the strings stored in the string array, we are comparing the alphabets starting from the first alphabet of each string to get them sorted in the alphabetical order. import java.util.Scanner; public class JavaExample { public static void main(String[] args) { int count; String temp; Scanner scan = new Scanner(System.in); //User will be asked to enter the count of strings System.out.print("Enter number of strings you would like to enter:"); count = scan.nextInt(); String str[] = new String[count]; Scanner scan2 = new Scanner(System.in); //User is entering the strings and they ar
🌐
Reddit
reddit.com › r/javahelp › how to sort a list alphabetically and place any items that begin with a number at the bottom
r/javahelp on Reddit: How to sort a list alphabetically and place any items that begin with a number at the bottom
April 13, 2020 -

Given a list, in my example it's an array list, I need to sort it alphabetically (case insensitive). The values in my list either begin with a letter, or a number. The numbers are supposed to be placed at the bottom of the sorted list.

This is a one-line task, except for the last part.

You can easily do it with:

Collections.sort(list, String.CASE_INSENSITIVE_ORDER);

The problem with this is that it places items beginning with a number at the top of the list.

What I want is:

["Alpha", "Bravo", "Delta", "1", "2"]

What I get is:

["1", "2", "Alpha", "Bravo", "Delta"]

What's the proper way to do this? I could always chop the list and append the number items to the bottom, but that seems wrong.

🌐
LabEx
labex.io › tutorials › java-how-to-sort-a-set-in-java-414138
How to sort a set in Java | LabEx
In this Java tutorial, you have learned how to sort a Set in Java using various methods, including the TreeSet and custom sorting. By understanding these techniques, you can effectively manage and organize your Java data structures, leading to more efficient and maintainable code.
🌐
Sanfoundry
sanfoundry.com › java-program-sort-names-alphabetical-order
Java Program to Sort Names in an Alphabetical Order - Sanfoundry
May 24, 2022 - $ javac Alphabetical_Order.java $ java Alphabetical_Order Enter number of names you want to enter:5 Enter all the names: bryan adam rock chris scott Names in Sorted Order:adam,bryan,chris,rock,scott
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-sort-a-string-in-java-alphabetically-in-java
How to Sort a String in Java alphabetically in Java?
April 22, 2025 - Following is an example to sort a string alphabetically - import java.util.Arrays; import java.util.Scanner; public class SortingString { public static void main(String args[]) { String str = "Hello welcome to Tutorialspoint"; char charArray[] = str.toCharArray(); Arrays.sort(charArray); System.out.println(new String(charArray)); } }
Top answer
1 of 16
158

A HashSet does not guarantee any order of its elements. If you need this guarantee, consider using a TreeSet to hold your elements.

However if you just need your elements sorted for this one occurrence, then just temporarily create a List and sort that:

Set<?> yourHashSet = new HashSet<>();

...

List<?> sortedList = new ArrayList<>(yourHashSet);
Collections.sort(sortedList);
2 of 16
94

Add all your objects to the TreeSet, you will get a sorted Set. Below is a raw example.

HashSet myHashSet = new HashSet();
myHashSet.add(1);
myHashSet.add(23);
myHashSet.add(45);
myHashSet.add(12);

TreeSet myTreeSet = new TreeSet();
myTreeSet.addAll(myHashSet);
System.out.println(myTreeSet); // Prints [1, 12, 23, 45]

Update

You can also use TreeSet's constructor that takes a HashSet as a parameter.

HashSet myHashSet = new HashSet();
myHashSet.add(1);
myHashSet.add(23);
myHashSet.add(45);
myHashSet.add(12);

TreeSet myTreeSet = new TreeSet(myHashSet);
System.out.println(myTreeSet); // Prints [1, 12, 23, 45]

Thanks @mounika for the update.

Java 8

Set<Integer> myHashSet = new HashSet<>();
myHashSet.add(1);
myHashSet.add(23);
myHashSet.add(45);
myHashSet.add(12);

LinkedHashSet<Integer> ascendingSortedSet = myHashSet.stream()
    .sorted()
    .collect(Collectors.toCollection(LinkedHashSet::new)); // Prints [1, 12, 23, 45]

LinkedHashSet<Integer> descendingSortedSet = myHashSet.stream()
    .sorted(Comparator.reverseOrder())
    .collect(Collectors.toCollection(LinkedHashSet::new)); // Prints [45, 23, 12, 1]
Top answer
1 of 1
1

The data that is present most closely resembles a map of header to rest of value. This should point one in the direction of a Map rather than a List.

Of the data that is presented, there are two groupings of the data - the first 13 fields, and all the rest. The presentation of the all the rest is to be used in a sorted order. For this, one looks at the SortedMap interface and sees the TreeMap as one if its implementations.

The 13 field data can be used as a little known map type - the EnumMap.

With the EnumMap, one would first define the enumerations of the fields in the order desired.

public enum Headers {
    SSN,
    NAME;
}

One then gets the associated code that looks something like (lacking the looping over the data):

SortedMap<String, String> other = new TreeMap<String, String>();
EnumMap<Headers, String> headers = new EnumMap<Headers, String>(Headers.class);
try {
    headers.put(Headers.valueOf(key), value);
} catch (IllegalArgumentException  e) {
    other.put(key, value);
}

If the header is present in the enum, put it in the headers map, otherwise put it in the other map.

At this point, one can then iterate over the headers EnumMap and then the other TreeMap printing out the key and value pairs. Or present a new object that itself extends Iterable and makes an iterator that first walks the EnumMap and then the SortedMap.

The enum provides easy extensibility of the code (if you want to do validation checking, one can associate it with the enum. See the Java tutorial on Enum Types to see some more things one can do with it (associate a method with the enum itself, associate values (Pattern? an instance of a class that has an interface providing a boolean validate(String arg)? display formatting code?) to further extend it.

🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › SortedSet.html
SortedSet (Java Platform SE 8 )
July 21, 2026 - Java™ Platform Standard Ed. 8 ... A Set that further provides a total ordering on its elements. The elements are ordered using their natural ordering, or by a Comparator typically provided at sorted set creation time. The set's iterator will traverse the set in ascending element order.