Lexicographical Order Java

The term Lexicographical order is a mathematical term known by names: lexical order, lexicographic(al) product, alphabetical order, or dictionary order.

This section will cover the topic lexicographical order, its definition, and other detailed information. After that, we will learn how to use the concept of lexicographical order in the Java programming language.

Defining Lexicographical Order

Lexicographical order or lexicographic in mathematics is a generalization of the alphabetical sequence of the dictionaries to the sequences of the ordered symbols or elements of a totally ordered list. The term lexicographical order is motivated by the word 'lexicon'. Lexicon is the set of words used in some of the other languages and has a conventional ordering. Thus, lexicographical order is a way for formalizing word order where the order of the underlying symbols is given.

In programming, lexicographical order is popularly known as Dictionary order and is used to sort a string array, compare two strings, or sorting array elements. It becomes quite easy to sort elements lexically. It is because lexicographical order has several variants and generalizations in which:

  • One variant is applicable to the sequences of different lengths as before considering the particular elements, lengths of the sequences are compared.
  • The second variant is used in order subsets of a given finite set. It does so by assigning a total order to the finite set. Then it is converting subsets into increasing sequences to which the lexicographical order is applied.
  • The generalization refers to the Cartesian product sequence of partially ordered sets, and such sequence is a total order, if and only if each factor of the Cartesian product are ordered totally.

Understanding the Formal Notion of Lexicographical Order

  • In order to understand the formal notion of the lexicographical order:
  • It begins with a finite set A, which is known as the alphabet and is completely sequenced. It further means that for a and b (any two symbols which are different and not the same) in A, either a < b or b < a.
  • Here, the words of A are the finite sequence of symbols from A and including words of length 1 holding a single symbol, words of length 2 with two symbols, and for words of length three, it's 3, and so on. With regards, it also includes the empty sequence ? holding no symbols at all. Thus the lexicographical order for the finite set A can be described as:
  • Suppose, for the two different worlds of the same length, a=a1a2…ak and b=b1b2…bk is given. Here, two words order depends on the alphabetic order of the symbols in the first place i where two words vary when counting from the beginning of the words, i.e., satisfying the condition a < b if and only if ai < bi within the order of the alphabet A.
  • If two words have varied in length, the usual lexicographical order pads the word with shorter length with blanks at the end until both words become the same in length, and then the words are compared.

Implementing Lexicographical in Java

As discussed above that lexicographical order can be used either for comparing two strings or sorting the elements. Here, we will discuss both the methods and will Implement each.

Sorting Elements in Lexicographical Order

Arranging words in order is known as lexicographical order or also known as Dictionary order. It means that on applying lexicographical order, the words are ordered alphabetically as per their component alphabets. For sorting a string array in lexicographical order, we have the following two methods:

Method 1: Applying any sorting method

Below is the example code given that will let us understand that how we can perform sorting on elements in Lexicographical order:

Code Explanation:

In the above code, we have created a class Main within which the main () method is created.

  • A string has been initialized, holding some values to it, and each word will get printed as per for loop.
  • Then, we have implemented the main logic within another for loop with the help of which we can form the lexicographical order of the words given.
  • Finally, via for loop, the arranged words are printed on the screen.

On executing the above example code, we got the following output:

Lexicographical Order Java

From the output, we can analyze that the given sequence of the words was not in alphabetical order but after applying the lexicographical order code, we can see that every word is sequenced now in alphabetical order.

Method 2: Applying sort () function

The sort () method is available in the Arrays class within the util package.

Below is the example code given that will let us understand that how we can perform sorting on elements in Lexicographical order:

On executing the above output, we got the below-shown output:

Lexicographical Order Java

Comparing two strings using Lexicographical order in Java

For comparing two strings using Lexicographical order, we have the following two methods:

Using compareTo () method

Let's begin one by one:

Using compareTo () method

Below is an example implementation by which we can compare to strings lexicographically:

Code Explanation:

  • We have created a class StringExample where we have implemented the main () method.
  • We have initialized two strings, i.e., str1 and str2.
  • Next, using the compareTo () method, we have compared the strings str1 and str2.
  • After it, if the get_val value is found less than 0, it means str1 is greater than str2.
  • Else if the get_val value is equal to 0, it means both str1 and str2 strings are equal.
  • Else, both the strings str1 is less than str2.

Output:

Lexicographical Order Java

By creating a user-defined function

Below we have created a user-defined function using which we can compare two strings lexicographically. The code is as follows:

Output:

Lexicographical Order Java

Code Explanation:

  • We have created a Java class where we have initialized five strings.
  • Next, we have compared the first string with the second string, the second to the third-string, and so on..
  • For making the comparison, we have created a user-defined function compareString () whereby comparing the length and each character of the strings, and we got the results.

Therefore, in this way, we can make use of the lexicographical order in Java for performing such tasks.