Home > Java > javaTutorial > body text

Use java's Arrays.sort() function to sort a string array lexicographically

WBOY
Release: 2023-07-26 19:01:20
Original
2689 people have browsed it

Use Java's Arrays.sort() function to sort string arrays in lexicographic order

In daily development, we often encounter the need to sort string arrays, such as sorting string arrays in alphabetical order. Sort by name, or sort a set of file names lexicographically. Lexicographic sorting of string arrays can be easily achieved using Java's Arrays.sort() function.

Java's Arrays class provides some static methods to operate arrays, among which the sort() method can be used to sort an array. The following is a sample code for lexicographically sorting a string array using the Arrays.sort() function:

import java.util.Arrays;

public class StringArraySortExample {
    public static void main(String[] args) {
        // 定义一个字符串数组
        String[] names = {"Alice", "Bob", "Charlie", "David", "Eva"};

        // 使用Arrays.sort()函数对字符串数组进行字典序排序
        Arrays.sort(names);

        // 打印排序后的字符串数组
        for (String name : names) {
            System.out.println(name);
        }
    }
}
Copy after login

In the above sample code, we first define a string arraynames , which contains some names. Then, we use the Arrays.sort() function to lexicographically sort the names array. Finally, use a for loop to print out the sorted string array.

Run the above code, and the output result is as follows:

Alice
Bob
Charlie
David
Eva
Copy after login

You can see that the string array names is sorted in alphabetical order.

It should be noted that the sort() function uses lexicographic order by default. If you want to implement reverse sorting, you can use the overloaded method of the sort() function and pass in Collections.reverseOrder() as a parameter, as shown below:

Arrays.sort(names, Collections.reverseOrder());
Copy after login

In addition to lexicographically sorting string arrays, the sort() function can also sort other types of arrays, such as integer arrays, floating point arrays, etc. Just use the corresponding array as a parameter of the sort() function.

Summary:
Using the sort() function of the Arrays class can conveniently sort string arrays in lexicographic order. Just call the sort() function and pass in the string array as a parameter to get the sorted results. It should be noted that the sort() function sorts in lexicographic order by default. If you need to sort in reverse, you can use the overloaded method and pass in Collections.reverseOrder() as a parameter.

The above is the detailed content of Use java's Arrays.sort() function to sort a string array lexicographically. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template