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 |
is one of the most popular programming languages in the world today, and is widely used in applications ranging from web development to mobile application development Java was developed by James Gosling and his team at Sun Microsystems in 1990, 1990; for its simplicity, ease...
4 min read
The Ugly number is another special positive number in Java. If a number has only 2, 3, or 5 prime factors and by convention 1 is also included, the number is called Ugly number. Let's take some examples of Ugly numbers. 27 is not an Ugly number because...
8 min read
Templates play an important role in software development, providing a way to define reusable systems that can be tailored to specific needs. In Java, templates are often implemented through a combination of classes and interfaces. In this section, we will explore the steps of creating templates...
8 min read
The concept of inheritance represents an essential principle among the four fundamental aspects of Object-Oriented Programming (OOP) in Java . A subclass can receive all fields and methods from its superclass through the inheritance mechanism. The feature enables developers to reuse code blocks and create maintainable and expandable...
3 min read
A character preceded by a backslash (\) is known as escape sequence. It has special meaning to the compiler. When an escape sequence is encountered in a print statement, the compiler interprets it accordingly. These are used to signal an alternative interpretation of a sequence...
3 min read
Linking classes together is a crucial component of object-oriented programming (OOP), a complex web of interconnectivity. The three basic ideas that underpin these interactions are introduced by Java, a language well-known for its object-oriented principles: association, aggregation, and composition. These ideas not only serve as the...
8 min read
There are three different types of Java getInteger() methods which can be differentiated depending on its parameter. These are: Java Integer getInteger(String nm) Method Java Integer getInteger(String nm, int val) Method Java Integer getInteger(String nm, Integer val) Method 1. Java Integer getInteger(String nm) Method: The getInteger(String nm) method is a...
5 min read
Problem Statement The task entails counting all rows in a given matrix that are either arranged ascending or descending. A row is deemed sorted if all of its items follow a pattern that is neither decreasing (ascending) nor growing. For instance, [1,2,3,4] is in ascending order. On the...
4 min read
In order to read the provided characters into a CharBuffer instance, Java's Reader Class's read(CharBuffer) method is used. The java can obtain a customized buffer called a CharBuffer.nio package and is intended for the efficient storage and manipulation of character sequences. This approach makes managing character...
5 min read
Among other optimisations in Java development programming the object pooling pattern serves to handle efficient object reuse management, especially in costly object life cycle events. The following article explains Java object creation techniques for establishing multiple pools of objects with variable sizes while discussing system...
5 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