Arrays.sort()

时间:2022-09-27 22:15:15

今天在做一个按更新时间搜寻出某个目录里面的全部文件,因为自己写算法比較花费时间,干脆就用j2se提供的类Arrays提供的sort()方法,这样就比較省力。对于基本数据类型仅仅要Arrays.sort(数组)[“注:数组是声明为基本数据类型的数组,如int[]等”]

对于对象类型,要 implement Comparable,所以得重载 compareTo() 这个方法。有了这个方法,那么 Arrays.sort() 就能按照这种方法的回传值来作排序的根据。其实,基本数据类型也都有 implement Comparable 的,所以才可以这样子使用。一般而言,我们习惯的排序结果是由小到大,所以在 compareTo() 其中「大于」是回传正值,假设你希望得到的结果是由大到小,那么仅仅要 return 的部分改一下,也就行了。只是不建议这样子作,会减少程序可读性;况且,仅仅要把数组逆向 traversal 不就得到一样的结果啦...

import java.util.Arrays;
import java.util.Comparator;

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.print(arrayToSort[i]+",");
   }

// 调用数组的静态排序方法sort
  Arrays.sort(arrayToSort);

System.out.println();
   System.out.println("排序后:");
   for (int i = 0; i < arrayToSort.length; i++){
    System.out.print(arrayToSort[i]+",");
   }

}

/**
* 字符型数组排序demo
*/

public void sortStringArray() {
   String[] arrayToSort = new String[] { "Oscar", "Charlie", "Ryan","Adam", "David","aff","Aff" };
   System.out.println();
   System.out.println("字符型数组排序,排序前:");
   for (int i = 0; i < arrayToSort.length; i++){
    System.out.print(arrayToSort[i]+",");
   }

System.out.println();
   System.out.println("排序后:");

// 调用数组的静态排序方法sort
   Arrays.sort(arrayToSort);

for (int i = 0; i < arrayToSort.length; i++){
    System.out.print(arrayToSort[i]+",");
   }

}

/**
* 对象数组排序demo
*/

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();
   System.out.println("对象数组排序排序前:");

for (int i = 0; i < dogs.length; i++) {
    Dog dog = dogs[i];
    System.out.print(dog.getName()+"["+dog.getWeight()+"],");
   }

Arrays.sort(dogs, new ByWeightComparator());

System.out.println();
   System.out.println("排序后:");

for (int i = 0; i < dogs.length; i++) {
    Dog dog = dogs[i];
    System.out.print(dog.getName()+"["+dog.getWeight()+"],");
   }

}

public static void main(String[] args) {
   ArraySortDemo t = new ArraySortDemo();
   t.sortIntArray();
   t.sortStringArray();
   t.sortObjectArray();
 }

}

/**
* 定义了一个Dog类
*/

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;
}
}

/**
* 由于要对对象进行排序,所以要实现java.util.Comparator接口的compare(T o1, T o2)方法,在该方法中自己定义排序算法。
*/

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;
}
}

执行结果:

整数型数组排序,排序前:
48,5,89,80,81,23,45,16,2
排序后:
2,5,16,23,45,48,80,81,89
字符型数组排序,排序前:
Oscar,Charlie,Ryan,Adam,David,aff,Aff
排序后:
Adam,Aff,Charlie,David,Oscar,Ryan,aff
对象数组排序排序前:
dog1[1],dog2[4],dog3[5],dog4[2],dog5[3]
排序后:
dog1[1],dog4[2],dog5[3],dog2[4],dog3[5]