Arrays.sort()
public static void sort(int[] a) Sorts the specified array into ascending numerical order. Implementation note: The sorting algorithm is a Dual-Pivot Quicksort by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm offers O(n log(n)) performance on many data sets that cause other quicksorts to degrade to quadratic performance, and is typically faster than traditional (one-pivot) Quicksort implementations.
Parameters: a - the array to be sorted
code
public class ArrayDemo { public static void main(String[] args) { // initializing unsorted int array int iArr[] = {2, 1, 9, 6, 4}; // let us print all the elements available in list for (int number : iArr) { System.out.println("Number = " + number); } // sorting array Arrays.sort(iArr); // let us print all the elements available in list System.out.println("The sorted int array is:"); for (int number : iArr) { System.out.println("Number = " + number); } }}/*Output...Number = 2Number = 1Number = 9Number = 6Number = 4The sorted int array is:Number = 1Number = 2Number = 4Number = 6Number = 9*/
Collections.sort()
public static <T extends Comparable<? super T>> void sort(List<T> list) Sorts the specified list into ascending order, according to the natural ordering of its elements. All elements in the list must implement the Comparable interface. Furthermore, all elements in the list must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the list). This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort. The specified list must be modifiable, but need not be resizable. Implementation Note: This implementation defers to the List.sort(Comparator) method using the specified list and a null comparator. Type Parameters: T - the class of the objects in the list Parameters: list - the list to be sorted.
code
实现comparable接口
/** * 根据order对User排序 */ public class User implements Comparable{ private String name; private Integer order; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getOrder() { return order; } public void setOrder(Integer order) { this.order = order; } public int compareTo(User arg0) { return this.getOrder().compareTo(arg0.getOrder()); } } 测试一下: public class Test{ public static void main(String[] args) { User user1 = new User(); user1.setName("a"); user1.setOrder(1); User user2 = new User(); user2.setName("b"); user2.setOrder(2); List list = new ArrayList (); //此处add user2再add user1 list.add(user2); list.add(user1); Collections.sort(list); for(User u : list){ System.out.println(u.getName()); } } }
Collections.sort重载方法来实现
public class User { //此处无需实现Comparable接口 private String name; private Integer order; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getOrder() { return order; } public void setOrder(Integer order) { this.order = order; } } 主类中这样写即可(HastSet——>List——>sort进行排序): public class Test { public static void main(String[] args) { User user1 = new User(); user1.setName("a"); user1.setPrice(11); User user2 = new User(); user2.setName("b"); user2.setPrice(2); SetHset = new HashSet (); Hset.add(user2); Hset.add(user1); List list = new ArrayList (); list.addAll(Hset); Collections.sort(list,new Comparator (){ public int compare(User arg0, User arg1) { return arg0.getPrice().compareTo(arg1.getPrice()); } }); for(User u : list){ System.out.println(u.getName()); } } /*输出结果如下:ab*/