compareTo() Method In Java Example

Chapter: String Handling Last Updated: 09-06-2017 12:53:36 UTC

Program:

            /* ............... START ............... */
                
public class JavaCompareToExample {
	static String arr[] = { "Now", "is", "the", "time", "for", "all", "good", "men", "to", "come", "to", "the", "aid",
			"of", "their", "country" };

	public static void main(String args[]) {
		for (int j = 0; j < arr.length; j++) {
			for (int i = j + 1; i < arr.length; i++) {
				if (arr[i].compareTo(arr[j]) < 0) {
					String t = arr[j];

					arr[j] = arr[i];
					arr[i] = t;
				}
			}
			System.out.println(arr[j]);
		}
	}
}
                /* ............... END ............... */
        

Output

Now
aid
all
come
country
for
good
is
men
of
the
the
their
time
to
to

Notes:

  • The method compareTo() is used for comparing two strings lexicographically. Each character of both the strings is converted into a Unicode value for comparison. If both the strings are equal then this method returns 0 else it returns positive or negative value. The result is positive if the first string is lexicographically greater than the second string else the result would be negative.
  • If Value = 0 - argument string lexicographically equal to this string.
  • If Value < 0 - argument string lexicographically greater than this string.
  • If Value > 0 - value greater than 0 if the argument string lexicographically less than this string.

Tags

compareTo(), Java

Similar Programs Chapter Last Updated
Java StringBuffer CharAt String Handling 27-05-2017
Java StringBuilder EnsureCapacity String Handling 27-05-2017
Java StringBuffer Capacity Example String Handling 27-05-2017
Java Character At Particular Index String Handling 16-05-2017
Java Convert String To Array Example String Handling 16-05-2017
Java String Trim Method String Handling 15-05-2017
Java String Character Case Check String Handling 15-05-2017
Java Find All Substrings Of String String Handling 14-05-2017
Java String Conversion Using ValueOf String Handling 30-03-2017
Immutable String In Java String Handling 24-03-2017
Java Float To String Using toString() String Handling 05-11-2016
Java Program To Convert Integer To String Using toString() String Handling 05-11-2016
Java Reverse String Using Stack String Handling 24-10-2016
Java Integer To String Conversion String Handling 21-10-2016
Java Program To Check Isomorphic Strings String Handling 19-10-2016
Java String Unicode Code Point Example String Handling 28-08-2016
Concatenate Strings In Java Example String Handling 21-08-2016
String Optimization In Java String Handling 21-08-2016
Java String Split Example String Handling 20-08-2016
Java String Search String Handling 20-08-2016
Java String Replace String Handling 20-08-2016
Java String Remove Character Example String Handling 20-08-2016
Java String Last Occurance String Handling 20-08-2016
Java String Compare Example String Handling 20-08-2016
String Initialization In Java Example String Handling 20-06-2016
StringBuffer SubString In Java String Handling 09-05-2016
StringBuffer Reverse In Java String Handling 09-05-2016
StringBuffer Insert In Java String Handling 09-05-2016
StringBuffer Append In Java String Handling 09-05-2016
Java String isEmpty() String Handling 01-05-2016

1 2 3