java数组排序Arrays.sort,以及Comparator接口的用法

有的时候需要对数组里的element进行排序。当然可以自己编写合适的排序方法,但既然java包里有自带的Arrays.sort排序方法,在数组元素比较少的时候为何不用?

Sorting an Array 1. 数字排序  int[] intArray = new int[] { 4, 1, 3, -23 };
Arrays.sort(intArray);
输出: [-23, 1, 3, 4]
2. 字符串排序,先大写后小写 String[] strArray = new String[] { "z", "a", "C" };
Arrays.sort(strArray);
输出: [C, a, z]
3. 严格按字母表顺序排序,也就是忽略大小写排序 Case-insensitive sort
Arrays.sort(strArray, String.CASE_INSENSITIVE_ORDER);
输出: [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 demo1.client;
import java.util.Arrays;
import java.util.Comparator;

public class ArraySortDemo {
 <wbr> <wbr> <wbr><br>  <wbr> <wbr> <wbr> public void sortIntArray() {<br>  <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr> int[] arrayToSort = new int[] { 48, 5, 89, 80, 81, 23, 45, 16, 2 };<br>  <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr> System.out.println("排序前");<br>  <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr> for (int i = 0; i &lt; arrayToSort.length; i++)<br>  <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr> System.out.println(arrayToSort[i]);<br>  <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr> // 调用数组的静态排序方法sort<br>  <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr> Arrays.sort(arrayToSort);<br>  <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr> System.out.println("排序后");<br>  <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr> for (int i = 0; i &lt; arrayToSort.length; i++)<br>  <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr> System.out.println(arrayToSort[i]);<br>  <wbr> <wbr> <wbr> }<br><br>  <wbr> <wbr> <wbr><br>  <wbr> <wbr> <wbr> public void sortStringArray() {<br>  <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr> String[] arrayToSort = new String[] { "Oscar", "Charlie", "Ryan",<br>  <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr> "Adam", "David" };<br>  <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr> System.out.println("排序前");<br>  <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr> for (int i = 0; i &lt; arrayToSort.length; i++)<br>  <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr> System.out.println(arrayToSort[i]);<br>  <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr> System.out.println("排序后");<br>  <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr> //调用数组的静态排序方法sort<br>  <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr> Arrays.sort(arrayToSort);<br><br>  <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr> for (int i = 0; i &lt; arrayToSort.length; i++)<br>  <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr> System.out.println(arrayToSort[i]);<br>  <wbr> <wbr> <wbr> }<br><br>  <wbr> <wbr> <wbr><br>  <wbr> <wbr> <wbr> public void sortObjectArray() {<br>  <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr> Dog o1 = new Dog("dog1", 1);<br>  <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr> Dog o2 = new Dog("dog2", 4);<br>  <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr> Dog o3 = new Dog("dog3", 5);<br>  <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr> Dog o4 = new Dog("dog4", 2);<br>  <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr> Dog o5 = new Dog("dog5", 3);<br>  <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr> Dog[] dogs = new Dog[] { o1, o2, o3, o4, o5 };<br>  <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr> System.out.println("排序前");<br>  <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr> for (int i = 0; i &lt; dogs.length; i++) {<br>  <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr> Dog dog = dogs[i];<br>  <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr> System.out.println(dog.getName());<br>  <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr> }<br>  <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr> Arrays.sort(dogs, new ByWeightComparator());<br>  <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr> System.out.println("排序后:");<br>  <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr> for (int i = 0; i &lt; dogs.length; i++) {<br>  <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr>  <wbr> <wbr> <wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

你可能感兴趣的:(java,jdk,制造)