有的時候須要對數組裏的element進行排序。固然能夠本身編寫合適的排序方法,但既然java包裏有自帶的Arrays.sort排序方法,在數組元素比較少的時候爲什麼不用? java
Sorting an Array 1. 數字排序 int[] intArray = new int[] { 4, 1, 3, -23 };api
Arrays.sort(intArray);數組
輸出: [-23, 1, 3, 4]ide
2. 字符串排序,先大寫後小寫 String[] strArray = new String[] { "z", "a", "C" };this
Arrays.sort(strArray);對象
輸出: [C, a, z]排序
3. 嚴格按字母表順序排序,也就是忽略大小寫排序 Case-insensitive sort接口
Arrays.sort(strArray, String.CASE_INSENSITIVE_ORDER);element
輸出: [a, C, z]字符串
4. 反向排序, Reverse-order sort
Arrays.sort(strArray, Collections.reverseOrder());
輸出:[z, a, C]
5. 忽略大小寫反向排序 Case-insensitive reverse-order sort
Arrays.sort(strArray, String.CASE_INSENSITIVE_ORDER);
Collections.reverse(Arrays.asList(strArray));
輸出: [z, C, a]
java初學者最多見的錯誤思想,就是試圖去寫一些方法來完成數組的排序功能,其實,數組排序功能,在java的api裏面早已實現,咱們沒有必要去重複製造輪子。
Arrays類有一個靜態方法sort,利用這個方法咱們能夠傳入咱們要排序的數組進去排序,由於咱們傳入的是一個數組的引用,因此排序完成的結果也經過這個引用的來更改數組.對於整數、字符串排序,jdk提供了默認的實現,若是要對一個對象數組排序,則要本身實現java.util.Comparator接口。
package test; import java.util.Arrays; import java.util.Comparator; public class test { public void sortIntArray() { int[] arrayToSort = new int[] { 48, 5, 89, 80, 81, 23, 45, 16, 2 }; System.out.println("排序前"); for (int i = 0; i < arrayToSort.length; i++) System.out.println(arrayToSort[i]); // 調用數組的靜態排序方法sort Arrays.sort(arrayToSort); System.out.println("排序後"); for (int i = 0; i < arrayToSort.length; i++) System.out.println(arrayToSort[i]); } public void sortStringArray() { String[] arrayToSort = new String[] { "Oscar", "Charlie", "Ryan", "Adam", "David" }; System.out.println("排序前"); for (int i = 0; i < arrayToSort.length; i++) System.out.println(arrayToSort[i]); System.out.println("排序後"); // 調用數組的靜態排序方法sort Arrays.sort(arrayToSort); for (int i = 0; i < arrayToSort.length; i++) System.out.println(arrayToSort[i]); } public void sortObjectArray() { Dog o1 = new Dog("dog1", 1); Dog o2 = new Dog("dog2", 4); Dog o3 = new Dog("dog3", 5); Dog o4 = new Dog("dog4", 2); Dog o5 = new Dog("dog5", 3); Dog[] dogs = new Dog[] { o1, o2, o3, o4, o5 }; System.out.println("排序前"); for (int i = 0; i < dogs.length; i++) { Dog dog = dogs[i]; System.out.println(dog.getName()); } Arrays.sort(dogs, new ByWeightComparator()); System.out.println("排序後:"); for (int i = 0; i < dogs.length; i++) { Dog dog = dogs[i]; System.out.println(dog.getName()); } } public static void main(String[] args) { test t = new test(); t.sortIntArray(); t.sortStringArray(); t.sortObjectArray(); } } class Dog { private String name; private int weight; public Dog(String name, int weight) { this.setName(name); this.weight = weight; } public int getWeight() { return weight; } public void setWeight(int weight) { this.weight = weight; } public void setName(String name) { this.name = name; } public String getName() { return name; } } class ByWeightComparator implements Comparator { public final int compare(Object pFirst, Object pSecond) { int aFirstWeight = ((Dog) pFirst).getWeight(); int aSecondWeight = ((Dog) pSecond).getWeight(); int diff = aFirstWeight - aSecondWeight; if (diff > 0) return 1; if (diff < 0) return -1; else return 0; } }