How to Sort String Array in Java10 Sept 2024 | 4 min read In programming, sorting is important because it puts elements of an array in a certain order. The widely used order is alphabetical order or natural order. The sorting is used for canonicalizing (the process of converting data in the standard form) data and for producing a human-readable format. In this section, we will learn how to sort String array in Java using user-defined logic and Arrays. sort() method There are two ways to sort a string array in Java:
Using User Defined LogicWe can sort a string array by comparing each element with the rest elements. In the following example, we have done the same. We have used two for loops. The inner (second) for loop avoids the repetitions in comparison. If the condition (countries[i].compareTo(countries[j])>0) is true than 0, it performs the swapping and sorts the array. SortStringArrayExample1.java Output: [ Australia, America, Denmark, France, Germany, India, Italy, Netherlands, South-Africa, Yugoslavia, Zimbabwe] Using the Arrays.sort() MethodIn Java, Arrays is the class defined in the java.util package that provides sort() method to sort an array in ascending order. It uses Dual-Pivot Quicksort algorithm for sorting. Its complexity is O(n log(n)). It is a static method that parses an array as a parameter and does not return anything. We can invoke it directly by using the class name. It accepts an array of type int, float, double, long, char, byte. Syntax: Where a is an array to be short. Note: Like the Arrays class, the Collections class also provides the sort() method to sort the array. But there is a difference between them. The sort() method of the Arrays class works for primitive type while the sort() method of the Collections class works for objects Collections, such as LinkedList, ArrayList, etc.We can perform sorting in the following ways:
Sort String Array in Ascending Order or Alphabetical OrderThe ascending order arranges the elements in the lowest to highest order. It is also known as natural order or alphabetical order. Let's sort an array using the sort() method of the Arrays class. SortStringArrayExample2.java Output: [Apple, Apricot, Blackberry, Custard apple, Date, Fig, Mulberry, Naseberry, Orange, Plum, Tamarind, Wood apple] Sort String Array in Descending Order or Reverse Natural OrderUsing the reverseOrder() MethodJava Collections class provides the reverseOrder() method to sort the array in reverse-lexicographic order. It is a static method, so we can invoke it directly by using the class name. It does not parse any parameter. It returns a comparator that imposes the reverse of the natural ordering (ascending order). It means that the array sorts elements in the ascending order by using the sort() method, after that the reverseOrder() method reverses the natural ordering, and we get the sorted array in descending order. Syntax: Suppose, a[] is an array to be sort in the descending order. We will use the reverseOrder() method in the following way: Let's sort a string array in the descending order. SortStringArrayExample3.java Output: [Zimbabwe, Yugoslavia, South-Africa, Netherlands, Italy, India, Germany, France, Denmark, America, Australia] Next TopicJava Tutorial |
Before the development of computer or programming, people did their jobs manually. It used to take a lot of time but they had no choice. Then the computer era came, and now the jobs to be done were fed on the system. It considerably reduced...
2 min read
No suitable driver found for JDBC is an exception in Java that generally occurs when any driver is not found for making the database connectivity. In this section, we will discuss why we get such an error and what should be done to get rid of...
4 min read
In Java, instance methods and static methods are two significant types of methods. Each serves a different role in how methods are defined and invoked. Static Methods Static methods, also known as class methods, belong to the class itself rather than to any specific instance of the...
7 min read
In any programming language, the program needs identifiers for storing different values that can be used throughout the program. These identifiers are variables. Variable in Java A variable is a name assigned to a value that is stored inside the system memory. The value can be updated during...
4 min read
Image processing is a significant aspect of computer vision and is widely used in various applications such as medical imaging, security, and multimedia. One of the fundamental operations in image processing is converting a colored image to a grayscale image. Grayscale images are simpler and...
4 min read
In this section, we will learn what is an emirp number and also create Java programs to check if the given number is emirp or not. The emirp number Java program frequently asked in Java coding tests to check the logic of the programmer. Emirp Number A number...
2 min read
Comparing two or multiple excel workbooks is very common requirement specially for automated test scenarios. In this section, we will learn how to compare two excel workbooks or to verify if two workbooks have same data set. Comparing Excel Files We must not start comparing excel sheets at...
6 min read
Java programming language enables conversion of text to human recognizable speech using the inbuilt interfaces of Java Speech API. It is used to enhance the user experience and comfortability. The API defines a cross-platform API to support command and control recognizers and speech synthesizers. Text -...
9 min read
? Java, the sprawling Indonesian island renowned for its rich cultural heritage, has been a melting pot of diverse communities and ethnic groups throughout history. Among these groups, the Kalangs hold a significant place. The Kalangs were a distinct ethnic and cultural community that flourished in Java,...
3 min read
In the ious sections, we have discussed many pattern programs. In this section, we will create Java program to print ladder with n steps. Using for Loop The following program prints the ladder with the gap between two side rails being 3 spaces. LadderPatternExample1.java import java.util.Scanner; public class LadderPatternExample1 { public static void...
2 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India