有的时候需要对数组里的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 com.gjh.gee.arrays;
- import java.util.Arrays;
- public class ArraySortDemo {
- 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) {
- ArraySortDemo t = new ArraySortDemo();
- t.sortIntArray();
- t.sortStringArray();
- t.sortObjectArray();
- }
- }
- package com.gjh.gee.arrays;
- public 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;
- }
- }
- package com.gjh.gee.arrays;
- import java.util.Comparator;
- public 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;
- }
- }
下面补充一个Collections.sort排序,原理和Arrays.sort差不多。
- //一个POJO例子
- class User {
- String name;
- String age;
- public User(String name,String age){
- this.name=name;
- this.age=age;
- }
- public String getAge() {
- return age;
- }
- public void setAge(String age) {
- this.age = age;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
- //具体的比较类,实现Comparator接口
- import java.util.Comparator;
- import java.util.List;
- import java.util.ArrayList;
- import java.util.Collections;
- public class ComparatorUser implements Comparator{
- public int compare(Object arg0, Object arg1) {
- User user0=(User)arg0;
- User user1=(User)arg1;
- //首先比较年龄,如果年龄相同,则比较名字
- int flag=user0.getAge().compareTo(user1.getAge());
- if(flag==0){
- return user0.getName().compareTo(user1.getName());
- }else{
- return flag;
- }
- }
- }
- //测试类
- public class SortTest {
- public static void main(String[] args){
- List userlist=new ArrayList();
- userlist.add(new User("dd","4"));
- userlist.add(new User("aa","1"));
- userlist.add(new User("ee","5"));
- userlist.add(new User("bb","2"));
- userlist.add(new User("ff","5"));
- userlist.add(new User("cc","3"));
- userlist.add(new User("gg","6"));
- ComparatorUser comparator=new ComparatorUser();
- Collections.sort(userlist, comparator);
- for (int i=0;i<userlist.size();i++){
- User user_temp=(User)userlist.get(i);
- System.out.println(user_temp.getAge()+","+user_temp.getName());
- }
- }
- }
//首先年龄排序,如果年龄相同,则按名字排序
结果:
1, aa
2, bb
3, cc
4, dd
5, ee //注意:同样是5岁的人,则比较名字(ee,ff),然后排序
5, ff
6, gg