摘要:经常看见有人还在不厌其烦的用冒泡(最常见!)或是交换做排序,实际上用几行代码就可以既快又好地实现排序,不论是简单类型还是类,数组还是Java聚集(Collection)。
简单类型的排序
简单类型不外是byte, char, short, int, long, float, double等数据类型, 这些类型不能放在聚集中,只能使用数组。java.util.Arrays方法提供了对这些类型的sort方法(实际上还有很多其他有用的方法),下面是对一个简单的int数组排序:
int[] arr = {2, 3, 1,10,7,4};
System.out.print("before sort: ");
for (int i = 0; i< arr.length; i++)
System.out.print(arr[i] + " ");
System.out.println();
Arrays.sort(arr);
System.out.print("after sort: ");
for (int i = 0; i< arr.length; i++)
System.out.print(arr[i] + " ");
System.out.println();
输出结果:
before sort: 2 3 1 10 7 4
after sort: 1 2 3 4 7 10
我们看到排序结果是按照升序排列的,下面的排序都是如此。
对象的排序
对象可以放在数组里,同样调用Arrays.sort(Object[] arr)即可;也可以放到聚集里,用java.util.Collections的sort(List list)。注意不是list必须实现List接口而不仅仅是Collection接口。
但是这个类必须实现了java.lang.Comparable接口。这个接口只有一个方法:int compartTo(Object o),当本对象比传入的对象大时,返回一个正整数。 以类Programmer为例:
class Programmer implements Comparable{
private String name;
private String language;
private double pay;
public Programmer(String name, String language, double pay) {
this.name = name;
this.language = language;
this.pay = pay;
}
public int compareTo(Object o) {
Programmer other = (Programmer)o;
return (int)pay - (int)other.pay;
}
public String toString(){
return "{name: " + name + ", language: " + language + ", money: " + pay + "}";
}
}
对其进行排序:
ArrayList list = new ArrayList();
list.add(new Programmer("张三", "C", 12000));
list.add(new Programmer("李四", "Java", 200));
list.add(new Programmer("王五", "C++", 5000));
list.add(new Programmer("钱六", "VB", 3000));
System.out.println("before sort: " + list);
Collections.sort(list);
System.out.println("after sort: " + list);
输出:
before sort: [{name: 张三, language: C, money: 12000.0}, {name: 李四, language: Java, money: 200.0}, {name: 王五, language: C++, money: 5000.0}, {name: 钱六, language: VB, money: 3000.0}]
after sort: [{name: 李四, language: Java, money: 200.0}, {name: 钱六, language: VB, money: 3000.0}, {name: 王五, language: C++, money: 5000.0}, {name: 张三, language: C, money: 12000.0}]
够简单吧!查查Comparable的javadoc可以知道,有很多类已经实现了该接口,因此对这些类的排序几行代码就可以搞定。
最近看C#发现其中用System.Array.sort对数组排序,适用于所有实现了IComparable接口的对象,看来微软的借鉴能力还真是强啊!
对已有类进行排序
上面的方法有一个问题,就是一个类已经存在了,并且没有实现Comparable接口,使用一个子类进行封装?很麻烦(你可以对下面的例子试试)。还有一种情况就是对一个类没法实现多种排序。以File类为例,它实现了Comparable接口,但是是按照名称排序的。如果要按照大小排序,或者按修改时间排序呢?对这两种情况,使用java.util包的Comparator接口:
Arrays.sort(Object[] arr, Comparator com)
Collections.sort(Object[] arr, Comparator com)
Comparator接口的方法:
public int compare(Object o1, Object o2) 当o1比o2大时返回一个正整数
public boolean equals(Object obj) 判断obj与这个Comparator是否同一个对象
下面使用Comparator对文件实现了按文件大小或修改时间排序:
class FileUtils {
static class CompratorByLastModified implements Comparator {
public int compare(Object o1, Object o2) {
File file1 = (File)o1;
File file2 = (File)o2;
long diff = file1.lastModified() - file2.lastModified();
if (diff > 0)
return 1;
else if (diff == 0)
return 0;
else
return -1;
}
public boolean equals(Object obj){
return true; //简单做法
}
}
static class CompratorBySize implements Comparator {
public int compare(Object o1, Object o2) {
File file1 = (File)o1;
File file2 = (File)o2;
long diff = file1.length() - file2.length();
if (diff > 0)
return 1;
else if (diff == 0)
return 0;
else
return -1;
}
public boolean equals(Object obj){
return true; //简单做法
}
}
}
调用的示例:
File dir = new File("C:\\temp");
File[] files = dir.listFiles();
System.out.print("before sort: ");
for (int i = 0; i< files.length; i++)
System.out.print(files[i] + " ");
System.out.println();
Arrays.sort(files);
System.out.print("sort by name: ");
for (int i = 0; i< files.length; i++)
System.out.print(files[i] + " ");
System.out.println();
Arrays.sort(files, new FileUtils.CompratorBySize());
System.out.print("sort by size: ");
for (int i = 0; i< files.length; i++)
System.out.print(files[i] + " ");
System.out.println();
Arrays.sort(files, new FileUtils.CompratorByLastModified());
System.out.print("sort by last modified: ");
for (int i = 0; i< files.length; i++)
System.out.print(files[i] + " ");
System.out.println();
自己找个目录试一下吧。用这些Java类库中的方法,一般情况下应该是不用自己写排序算法了吧?
最后附上完整代码占点版面:
TestSort.java
import java.io.*;
import java.util.*;
public class TestSort {
public static void main(String[] args) {
sortSimpleType();
sortComparable();
sortComparator();
}
public static void sortSimpleType() {
int[] arr = {2, 3, 1,10,7,4};
System.out.print("before sort: ");
for (int i = 0; i< arr.length; i++)
System.out.print(arr[i] + " ");
System.out.println();
Arrays.sort(arr);
System.out.print("after sort: ");
for (int i = 0; i< arr.length; i++)
System.out.print(arr[i] + " ");
System.out.println();
}
public static void sortComparable() {
ArrayList list = new ArrayList();
list.add(new Programmer("张三", "C", 12000));
list.add(new Programmer("李四", "Java", 200));
list.add(new Programmer("王五", "C++", 5000));
list.add(new Programmer("钱六", "VB", 3000));
System.out.println("before sort: " + list);
Collections.sort(list);
System.out.println("after sort: " + list);
}
public static void sortComparator() {
File dir = new File("C:\\temp");
File[] files = dir.listFiles();
System.out.print("before sort: ");
for (int i = 0; i< files.length; i++)
System.out.print(files[i] + " ");
System.out.println();
Arrays.sort(files);
System.out.print("sort by name: ");
for (int i = 0; i< files.length; i++)
System.out.print(files[i] + " ");
System.out.println();
Arrays.sort(files, new FileUtils.CompratorBySize());
System.out.print("sort by size: ");
for (int i = 0; i< files.length; i++)
System.out.print(files[i] + " ");
System.out.println();
Arrays.sort(files, new FileUtils.CompratorByLastModified());
System.out.print("sort by last modified: ");
for (int i = 0; i< files.length; i++)
System.out.print(files[i] + " ");
System.out.println();
}
}
class Programmer implements Comparable{
private String name;
private String language;
private double pay;
public Programmer(String name, String language, double pay) {
this.name = name;
this.language = language;
this.pay = pay;
}
public int compareTo(Object o) {
Programmer other = (Programmer)o;
return (int)pay - (int)other.pay;
}
public String toString(){
return "{name: " + name + ", language: " + language + ", money: " + pay + "}";
}
}
class FileUtils {
static class CompratorByLastModified implements Comparator {
public int compare(Object o1, Object o2) {
File file1 = (File)o1;
File file2 = (File)o2;
long diff = file1.lastModified() - file2.lastModified();
if (diff > 0)
return 1;
else if (diff == 0)
return 0;
else
return -1;
}
public boolean equals(Object obj){
return true; //简单做法
}
}
static class CompratorBySize implements Comparator {
public int compare(Object o1, Object o2) {
File file1 = (File)o1;
File file2 = (File)o2;
long diff = file1.length() - file2.length();
if (diff > 0)
return 1;
else if (diff == 0)
return 0;
else
return -1;
}
public boolean equals(Object obj){
return true; //简单做法
}
}
}