How to Sort a String List Alphabetically
Sorting a list of strings alphabetically is a common operation in programming. Fortunately, Java provides a convenient way to achieve this task.
Solution:
To sort a List
Code Example:
<code class="java">import java.util.Collections; import java.util.List; public class SortStringList { public static void main(String[] args) { // Create a list of country names List<String> listOfCountryNames = List.of("United States", "Canada", "Mexico", "Brazil", "Argentina"); // Sort the list alphabetically Collections.sort(listOfCountryNames); // Print the sorted list System.out.println(listOfCountryNames); } }</code>
Output:
[Argentina, Brazil, Canada, Mexico, United States]
By using Collections.sort, you can easily sort a list of strings alphabetically, providing a convenient and efficient way to organize your string data.
The above is the detailed content of How to Sort a String List Alphabetically in Java?. For more information, please follow other related articles on the PHP Chinese website!