java中的数组与集合的排序
两种需要排序的对象:数组和集合
1、集合
java.util.Collections类提供了排序方法sort();
static void |
sort(List list) Sorts the specified list into ascending order, according to the natural ordering of its elements. |
static void |
sort(List list, Comparator c) Sorts the specified list according to the order induced by the specified comparator. |
两种排序方式:
1)被排序对象实现java.lang.Comparable接口,在其中的compareTo()方法中确定排序规则
2)用java.util.Comparator的具体实例确定排序规则
2、数组
java.util.Arrays类提供了各种重载的排序方法sort();
static void |
sort(byte[] a) Sorts the specified array of bytes into ascending numerical order. |
static void |
sort(byte[] a, int fromIndex, int toIndex) Sorts the specified range of the specified array of bytes into ascending numerical order. |
static void |
sort(char[] a) Sorts the specified array of chars into ascending numerical order. |
static void |
sort(char[] a, int fromIndex, int toIndex) Sorts the specified range of the specified array of chars into ascending numerical order. |
static void |
sort(double[] a) Sorts the specified array of doubles into ascending numerical order. |
static void |
sort(double[] a, int fromIndex, int toIndex) Sorts the specified range of the specified array of doubles into ascending numerical order. |
static void |
sort(float[] a) Sorts the specified array of floats into ascending numerical order. |
static void |
sort(float[] a, int fromIndex, int toIndex) Sorts the specified range of the specified array of floats into ascending numerical order. |
static void |
sort(int[] a) Sorts the specified array of ints into ascending numerical order. |
static void |
sort(int[] a, int fromIndex, int toIndex) Sorts the specified range of the specified array of ints into ascending numerical order. |
static void |
sort(long[] a) Sorts the specified array of longs into ascending numerical order. |
static void |
sort(long[] a, int fromIndex, int toIndex) Sorts the specified range of the specified array of longs into ascending numerical order. |
static void |
sort(Object[] a) Sorts the specified array of objects into ascending order, according to the natural ordering of its elements. |
static void |
sort(Object[] a, Comparator c) Sorts the specified array of objects according to the order induced by the specified comparator. |
static void |
sort(Object[] a, int fromIndex, int toIndex) Sorts the specified range of the specified array of objects into ascending order, according to the natural ordering of its elements. |
static void |
sort(Object[] a, int fromIndex, int toIndex, Comparator c) Sorts the specified range of the specified array of objects according to the order induced by the specified comparator. |
static void |
sort(short[] a) Sorts the specified array of shorts into ascending numerical order. |
static void |
sort(short[] a, int fromIndex, int toIndex) Sorts the specified range of the specified array of shorts into ascending numerical order. |
如果对int[]等类型可直接升序排序
如果对Object[]类型排序,需要确定排序顺序,仍旧两种方式,被排序类本身实现Comparable接口或者提供java.util.Comparator类。
/* import java.util.ArrayList; /** private void sortByComparableOfList(){ /** 根据Comparable接口确定排序顺序 */
/* (non-Javadoc) } |
程序执行结果:
-- sort list by comparable -- <List.3>:<0> <List.9>:<0> <List.0>:<1> <List.1>:<1> <List.4>:<1> <List.6>:<3> <List.8>:<3> <List.5>:<7> <List.2>:<8> <List.7>:<9> -- sort list by comparator -- <List.7>:<5> <List.1>:<4> <List.2>:<3> <List.8>:<3> <List.3>:<2> <List.5>:<2> <List.9>:<2> <List.0>:<0> <List.4>:<0> <List.6>:<0> -- sort array by default -- <Array0>:<1> <Array1>:<2> <Array2>:<2> <Array3>:<6> <Array4>:<7> <Array5>:<8> <Array6>:<8> <Array7>:<9> <Array8>:<9> <Array9>:<9> -- sort array by comparable -- <Array2>:<0> <Array1>:<1> <Array7>:<1> <Array9>:<3> <Array0>:<4> <Array8>:<4> <Array5>:<5> <Array6>:<5> <Array3>:<8> <Array4>:<9> -- sort array by comparator -- <Array3>:<9> <Array7>:<9> <Array1>:<7> <Array4>:<5> <Array5>:<3> <Array8>:<3> <Array0>:<2> <Array2>:<1> <Array6>:<1> <Array9>:<0> |