Solution for Java 8 based on java.util.Comparator.comparing(...):

Comparator<String> c = Comparator.comparing(String::toString);

or

Comparator<String> c = Comparator.comparing((String x) -> x);
Answer from Anthony on Stack Overflow
🌐
W3Schools
w3schools.com › java › ref_string_compareto.asp
Java String compareTo() Method
A value less than 0 is returned if the string is less than the other string (less characters) and a value greater than 0 if the string is greater than the other string (more characters). Tip: Use compareToIgnoreCase() to compare two strings lexicographyically, ignoring lower case and upper case differences.
Discussions

Need help passing a comparator as a parameter
new MySortedLinkedList<String>(Comparator<String>);

When passing a parameter, you need to use an object instance, not an interface/class type. If you must use a comparator (if the MySortedLinkedList class has no support for using the natural ordering of Comparable objects), then you can make a "natural ordering" comparator:

private static class NaturalOrderingComparator<T extends Comparable<T>> implements Comparator<T>
{
   private static final Comparator<?> INSTANCE = new NaturalOrderingComparator<String>();
   private NaturalOrderingComparator(){} // singleton

   public static <U extends Comparable<U>> Comparator<U> getInstance()
   {
      @SuppressWarnings("unchecked")
      Comparator<U> comparator = (Comparator<U>) INSTANCE;
      return comparator;
   }
   
   public int compare(T a, T b)
   {
      return a.compareTo(b);
   }
}

And then use that:

MySortedLinkedList<String> t = new MySortedLinkedList<String>(NaturalOrderingComparator.getInstance());
More on reddit.com
🌐 r/java
6
0
May 31, 2010
It seems that Comparator doesn't work on java stream, while it works with method Collections.sort
Edit: I found what was the problem! Apparently collecting into a set, breaks the order More on reddit.com
🌐 r/learnjava
5
1
March 15, 2024
Noob Programmer, ELI5: Comparators?
You can implement the Comparator method on a class in order to enable alternative sorting or searching methods through the java library. For instance, if you wanted to sort an ArrayList of type String based on String length, then you could write a class called StringLengthComparator that implements Comparator with the desired comparison. StringLengthComparator slc = new StringLengthComparator(); int result = slc.compare(a, b); If a is shorter than b, then the method would return a number less than zero. If it is longer than b, then it would return a number greater than zero. If they are the same length, then it would return zero. You could then sort the ArrayList using Collections.sort. Collections.sort(myArrayList, slc); More on reddit.com
🌐 r/java
17
1
October 23, 2014
How to properly compare Strings in Java
In Java, Strings are Objects, not (as commonly assumed, or as in other languages) primitive types. For Objects, "==" compares the Object references (pointers to the memory locations of the actual objects), not the contents. More on reddit.com
🌐 r/javahelp
April 8, 2020
🌐
Baeldung
baeldung.com › home › java › java string › comparing strings in java
Comparing Strings in Java | Baeldung
June 19, 2024 - The method returns true if two Strings are equal by first comparing them using their address i.e “==”. Consequently, if both arguments are null, it returns true and if exactly one argument is null, it returns false.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-comparator-interface
Java Comparator Interface - GeeksforGeeks
The compare() method compares two elements and returns: -1 -> if the first element should come before the second ... import java.util.*; // Define the Student class class Student{ String name; Integer age; Student(String name, Integer age) { ...
Published   1 month ago
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Comparator.html
Comparator (Java Platform SE 8 )
October 20, 2025 - The returned comparator is serializable if the specified comparator is also serializable. ... For example, to sort a collection of String based on the length and then case-insensitive natural ordering, the comparator can be composed using following code,
🌐
GeeksforGeeks
geeksforgeeks.org › java › compare-two-strings-in-java
Compare two Strings in Java - GeeksforGeeks
July 11, 2025 - Define a function to compare values with the following conditions : if (string1 > string2) it returns a positive value. if both the strings are equal lexicographically i.e.(string1 == string2) it returns 0. if (string1 < string2) it returns ...
Find elsewhere
🌐
Medium
medium.com › @AlexanderObregon › javas-comparator-comparing-method-explained-342361288af6
Java’s Comparator.comparing() Method Explained | Medium
October 2, 2024 - Sorting by name is one of the most common tasks when working with lists of objects. With Comparator.comparing(), you can specify that the comparison should be based on the name field, which is a String. Java will handle sorting alphabetically:
🌐
Dev.java
dev.java › learn › writing-and-combining-comparators
Writing and Combining Comparators
Getting to know the basics of the Java language. ... Defining your own classes, declaring member variables, methods, and constructors. ... How to model your immutable data with records to make your code simpler and more readable. ... Understanding numbers, characters and strings of characters.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › String.html
String (Java Platform SE 8 )
October 20, 2025 - A Comparator that orders String objects as by compareToIgnoreCase. This comparator is serializable. Note that this Comparator does not take locale into account, and will result in an unsatisfactory ordering for certain locales. The java.text package provides Collators to allow locale-sensitive ordering.
🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › String › compareTo
Java String compareTo() - Compare Strings Alphabetically | Vultr Docs
May 15, 2025 - The compareTo() method in Java is a versatile tool for determining the lexicographical order of strings, useful in sorting and comparisons that require a precise order of elements.
🌐
Sentry
sentry.io › sentry answers › java › how to compare strings in java
How to compare strings in Java | Sentry
public class Main { public static ... than another string (that is, whether it comes before or after alphabetically), use String.compareTo()....
🌐
W3Schools
w3schools.com › java › java_advanced_sorting.asp
Java Advanced Sorting (Comparator and Comparable)
The Comparator and Comparable interfaces allow you to specify what rule is used to sort objects. Being able to specify a sorting rule also allows you to change how strings and numbers are sorted.
🌐
Java Mex
javamex.com › tutorials › collections › sorting_comparator_example.shtml
Specifying how to sort data in Java: an example Comparator
That is– as discussed when we reviewed sorting_comparable_compareto.shtml the compareTo() method– it should return: a negative number if (and only if) o1 comes before o2; a positive number if (and only if) o1 comes after o2; ... public class StringLengthComparator implements Comparator<String> { public int compare(String o1, String o2) { if (o1.length() < o2.length()) { return -1; } else if (o1.length() > o2.length()) { return 1; } else { return 0; } } }
🌐
InfoWorld
infoworld.com › home › blogs › java challengers
Sorting Java objects with Comparable and Comparator | InfoWorld
May 23, 2024 - We’ll start with how to sort using Java’s Comparable interface. We use Comparable when there is a single, default comparison for the object we want sorted. In this first example, we implement Comparable in a Simpson class, using Simpson in the generic type: class Simpson implements Comparable<Simpson> { String name; Simpson(String name) { this.name = name; } @Override public int compareTo(Simpson simpson) { return this.name.compareTo(simpson.name); } } public class SimpsonSorting { public static void main(String...
🌐
Medium
medium.com › @alxkm › java-string-comparison-a-complete-guide-44df959a756f
Java String Comparison — A Complete Guide | by Alex Klimenko | Medium
July 11, 2025 - It is case-sensitive and null-safe only if used properly (e.g., "value".equals(str)). The .compareTo() method is used to perform a lexicographical (dictionary-style) comparison, returning 0 if the strings are equal, a negative number if the ...
🌐
Quora
quora.com › How-do-you-compare-two-String-objects-in-Java
How to compare two String objects in Java - Quora
Answer (1 of 2): Aside from what was described here https://qr.ae/pvSFR1 There are two other ways to compare objects: 1. By implementing the Comparable interface and using [code ]compareTo(T o)[/code] method. 2. By implementing the Comparator Interface and using one (or more) of its methods. Yo...
🌐
Reddit
reddit.com › r/java › need help passing a comparator as a parameter
r/java on Reddit: Need help passing a comparator as a parameter
May 31, 2010 -

I am creating a sorted LinkedList class, and the parameter should be a Comparator. This is my initializing line:

MySortedLinkedList[String] t = new MySortedLinkedList[String] (Comparator[String]);

I am using < in the actual code, but I switched to square brackets because reddit hates them, for some reason. Anyway, when I type that line in, I get this exception in Eclipse:

Syntax error on token ">", Expression expected after this.

How do I fix the error? I do not want to create another comparator when the string one works fine.

🌐
Text Compare
text-compare.com
Text Compare! - Find differences between two text files
To use Text Compare!, simply paste your two text versions into the provided fields. Our tool highlights additions, deletions, and modifications, making it easy to track changes. Please note that the text is securely sent to our server over an encrypted connection for comparison, but it is not ...
🌐
Cogentuniversity
cogentuniversity.com › post › string-comparison-in-java
String Comparison in Java
October 30, 2023 - In this example, we define a user-defined function named compareStrings. This function encapsulates the comparison logic utilizing the equals() method. Within the main method, we call compareStrings and provide two strings, "Java" and "Java", for comparison.
🌐
Opensource.com
opensource.com › article › 19 › 9 › compare-strings-java
How to compare strings in Java | Opensource.com
September 20, 2019 - The string class has a String equals method to compare two strings. String comparison with equals is case-sensitive.