可以使用java.util.Arrays.sort()方法以区分大小写的顺序对数组进行排序。在这种情况下,此方法仅需要单个参数,即要排序的数组。演示此的程序如下所示-
import java.util.Arrays; public class Demo { public static void main(String args[]) { String[] arr = new String[] { "apple", "mango", "Banana", "Melon", "orange" }; System.out.print("The unsorted array is: "); System.out.println(Arrays.toString(arr)); Arrays.sort(arr); System.out.print("The sorted array in case-sensitive order is: "); System.out.println(Arrays.toString(arr)); } }
输出结果
The unsorted array is: [apple, mango, Banana, Melon, orange] The sorted array in case-sensitive order is: [Banana, Melon, apple, mango, orange]
现在让我们了解上面的程序。
首先定义数组arr []。然后打印未排序的数组。演示这的代码片段如下-
String[] arr = new String[] { "apple", "mango", "Banana", "Melon", "orange" }; System.out.print("The unsorted array is: "); System.out.println(Arrays.toString(arr));
Arrays.sort()方法用于区分大小写的数组排序。然后显示排序后的数组。演示这的代码片段如下-
Arrays.sort(arr); System.out.print("The sorted array in case-sensitive order is: "); System.out.println(Arrays.toString(arr));