Lexicographical Order Java

Lexicographical Order Java Program. A dictionary is a book that describes the meanings of words, or lexemes, in more detail. The terms are organised alphabetically to make it easier to find them

class Main
{
public static void main(String[] args)
{
String[] words = { "Swift", "C program", "Python", "Java" };
for(int i = 0; i < 3; ++i)
{
for (int j = i + 1; j < 4; ++j)
{
if (words[i].compareTo(words[j]) > 0)
{
String temp = words[i];
words[i] = words[j];
words[j] = temp;
}
}
}
System.out.println("In dictionary order");
for(int i = 0; i < 4; i++)
{
System.out.println(words[i]);
}
}
}

 

Output:

In dictionary order
C program
Java
Python
Swift

Lexicographical Order in Java Program

Sorting is the process of placing items in a specific order on a list or in a group. Sorting can be done in ascending or descending order. Sorting is the process of arranging data in ascending or descending order based on a linear relationship between the data items.

A dictionary is a book of words that describes the functions of individual words, which are referred to as lexical items. It does so by listing these words in alphabetical order as headwords, with the words listed as dictionary entries.

What is compareTo in Java Program

compareTo in Java String () The compareTo() method of the Java String class compares the given string to the current string lexicographically. It returns a positive, negative, or zero value. It compares strings based on the Unicode value assigned to each character in the strings. 

What is String Temp in Java Program

temp is just a name for a local variable, not a keyword. To give this variable a meaningful name, you can call it temp, blub, or even better: entry or value. A temporary variable is a variable in computer programming that has a short lifetime and is used to hold data that will be discarded soon or before it can be stored in a more permanent memory location. It’s usually declared as a local variable, or a variable with local scope, because it’s short-lived. Lexicographical Order Java this is program were we can check the string in lexicographical order in java.

Similar Programs