List排序
1、使用Collections的sort(List<T> list)方法对List集合进行从小到大排序
/** * 使用Collections的sort(List<T> list)方法对List集合进行从小到大排序 */ @Test public void listDefaultSort() { List<Integer> list = new ArrayList<Integer>(); list.add(1); list.add(3); list.add(2); System.out.println(list); System.out.println("------------------"); Collections.sort(list); // 按从小到大排序,只能对基本数据类型的包装对象 System.out.println(list); }
执行结果:
[1, 3, 2] ------------------ [1, 2, 3]
2、使用Collections的sort(List<T> list, Comparator<? super T> c)方法对List集合进行自定义排序
/** * 使用Collections的sort(List<T> list, Comparator<? super T> c)方法对List集合进行自定义排序 */ @Test public void listCustomSort() { List<Person> list = new ArrayList<Person>(); Person p1 = new Person("1", "p1" , 12); Person p2 = new Person("2", "p2" , 9); Person p3 = new Person("3", "p3" , 13); Person p4 = new Person("4", "p4" , 9); list.add(p1); list.add(p2); list.add(p3); list.add(p4); System.out.println(list); System.out.println("------------------"); Collections.sort(list, new Comparator<Person>() { // 按年龄从大到小排序 @Override public int compare(Person p1, Person p2) { return p1.getAge() == p2.getAge() ? 0 : (p1.getAge() < p2.getAge() ? 1 : -1); } }); System.out.println(list); }
class Person { private String id; private String name; private int age; public Person(String id, String name, int age) { super(); this.id = id; this.name = name; this.age = age; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "\n Person [id=" + id + ", name=" + name + ", age=" + age + "]"; } }
执行结果:
[ Person [id=1, name=p1, age=12], Person [id=2, name=p2, age=9], Person [id=3, name=p3, age=13], Person [id=4, name=p4, age=9]] ------------------ [ Person [id=3, name=p3, age=13], Person [id=1, name=p1, age=12], Person [id=2, name=p2, age=9], Person [id=4, name=p4, age=9]]
数组排序
1、使用Arrays.sort(int[] a)方法对数组按从小到大排序
/** * 使用Arrays.sort(int[] a)方法对数组按从小到大排序 */ @Test public void arrayDefaultSort() { int[] array = {3, 4, 6, 1, 8, 2, 1, 5, 9, 0}; for (int i : array) { System.out.print(i); System.out.print(" "); } System.out.println(""); System.out.println("---------------------"); Arrays.sort(array); // 按从小到大排序,只能对基本数据类型以及其封装对象 for (int i : array) { System.out.print(i); System.out.print(" "); } }
执行结果:
3 4 6 1 8 2 1 5 9 0 --------------------- 0 1 1 2 3 4 5 6 8 9
2、使用Arrays.sort(int[] a, int fromIndex, int toIndex)部分从小到大排序
/** * 使用Arrays.sort(int[] a, int fromIndex, int toIndex)部分从小到大排序 * fromIndex 要排序的第一位索引(包括,从这开始) * toIndex 要排序的最后一个索引(不包括在内,在这结束) */ @Test public void arrayPartSort() { Integer[] array = {3, 4, 6, 1, 8, 2, 1, 5, 9, 0}; for (int i : array) { System.out.print(i); System.out.print(" "); } System.out.println(""); System.out.println("---------------------"); Arrays.sort(array, 3, 6); // 对下标为3至6(不包括6)按从小到大排序,只能对基本数据类型以及其封装对象 for (int i : array) { System.out.print(i); System.out.print(" "); } }
执行结果:
扫描二维码关注公众号,回复:
6787947 查看本文章
3 4 6 1 8 2 1 5 9 0 --------------------- 3 4 6 1 2 8 1 5 9 0
3、使用Arrays.sort(T[] a, Comparator<? super T> c)自定义排序
/** * 使用Arrays.sort(T[] a, Comparator<? super T> c)自定义排序 */ @Test public void arrayCustomSort() { Integer[] array = {3, 4, 6, 1, 8, 2, 1, 5, 9, 0}; for (Integer i : array) { System.out.print(i); System.out.print(" "); } System.out.println(""); System.out.println("---------------------"); Arrays.sort(array, new Comparator<Integer>() { // 从大到小排序 @Override public int compare(Integer i1, Integer i2) { return i1 == i2 ? 0 : (i1 < i2 ? 1 : -1); } }); for (int i : array) { System.out.print(i); System.out.print(" "); } }
执行结果:
3 4 6 1 8 2 1 5 9 0 --------------------- 9 8 6 5 4 3 2 1 1 0
常见排序算法
1、冒泡排序
2、选择排序
3、插入排序
4、希尔排序
5、归并排序
6、快速排序
7、堆排序
8、计数排序
9、桶排序
10、基数排序