Write a Java Program to Sort Elements in Lexicographical Order (Dictionary Order)

Java Program to Sort Elements in Lexicographical Order (Dictionary Order)

Welcome to the World of Online Learning:

Hello Friends “This blog helps you to learn Java programming concepts. You can learn Java  language at your own speed and time. One can learn concepts of Java language by practicing various programs given on various pages of this blog. Enjoy the power of Self-learning using the Internet.”

Java Program to Sort Elements in Lexicographical Order (Dictionary Order)
Java Program to Sort Elements in Lexicographical Order (Dictionary Order)

Write a Java Program to Sort Elements in Lexicographical Order (Dictionary Order)

PROGRAM: Java Program to Sort Elements in Lexicographical Order (Dictionary Order)

/*Java Program to Sort Elements in Lexicographical Order (Dictionary Order)*/

class Main {
public static void main(String[] args) {

String[] words = { “Ruby”, “C”, “Python”, “Java” };

for(int i = 0; i < 3; ++i) {
for (int j = i + 1; j < 4; ++j) {

if (words[i].compareTo(words[j]) > 0) {

// swap words[i] with words[j[
String temp = words[i];
words[i] = words[j];
words[j] = temp;
}
}
}

System.out.println(“In lexicographical order:”);

for(int i = 0; i < 4; i++) {
System.out.println(words[i]);
}
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *