compare(a,b)方法:根据第一个参数小于、等于或大于第二个参数分别返回负整数、零或正整数。
equals(obj)方法:仅当指定的对象也是一个 Comparator,并且强行实施与此 Comparator 相同的排序时才返回 true。
Collections.sort(list, new PriceComparator());的第二个参数返回一个int型的值,就相当于一个标志,告诉sort方法按什么顺序来对list进行排序。
具体实现代码方法如下:
Book实体类:
<pre class="java" name="code">import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.TreeMap;
public class Book implements Comparable {
public int id;
public String name;
public double price;
private String author;
public GregorianCalendar calendar;
public Book() {
this(0, "X", 0.0, new GregorianCalendar(), "");
}
public Book(int id, String name, double price, GregorianCalendar calender,
String author) {
this.id = id;
this.name = name;
this.price = price;
this.calendar = calender;
this.author = author;
}
<span style="color:#ff0000;"> // override toString() of parent Object
</span> public String toString() {
String showStr = id + "\t" + name;
DecimalFormat formatPrice = new DecimalFormat("0.00");// 2 decimal places
showStr += "\t" + formatPrice.format(price);
showStr += "\t" + author;
SimpleDateFormat formatDate = new SimpleDateFormat("yyyy.MM.dd");
showStr += "\t" + formatDate.format(calendar.getTime());
return showStr;
}
public int compareTo(Object obj) {// method in Comparable
Book b = (Book) obj;
return this.id - b.id; // comparation based on book id. it for default sorting
}
public static void main(String[] args) {
Book b1 = new Book(10000, "hong lou meng", 150.86, new GregorianCalendar(2009,
01, 25), "cao xueqin, gao e ");
Book b2 = new Book(10001, "san guo yan yi", 99.68, new GregorianCalendar(2008, 7,
8), "luo guanzhong ");
Book b3 = new Book(10002, "shui fu zhuan", 100.8, new GregorianCalendar(2009, 6,
28), "shi na an ");
Book b4 = new Book(10003, "xi you ji", 120.8, new GregorianCalendar(2011, 6,
8), "wu cheng en");
Book b5 = new Book(10004, "tian long ba bu", 10.4, new GregorianCalendar(2011, 9,
23), "sou hu");
TreeMap tm = new TreeMap();
tm.put(b1, new Integer(255));
tm.put(b2, new Integer(122));
tm.put(b3, new Integer(688));
tm.put(b4, new Integer(453));
tm.put(b5, new Integer(40));
Iterator it = tm.keySet().iterator();
Object key = null, value = null;
Book bb = null;
while (it.hasNext()) {
key = it.next();
bb = (Book) key;
value = tm.get(key);
System.out.println(bb.toString() + "\tkucun:" + tm.get(key));
}
}
}
UseComparator class
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
public class UseComparator {
public static void main(String args[]) {
List<Book> list = new ArrayList<Book>(); // array list
Book b1 = new Book(10000, "hongloumeng", 150.86, new GregorianCalendar(2009,
01, 25), "caoxueqin, gaoe ");
Book b2 = new Book(10001, "sanguoyanyi", 99.68, new GregorianCalendar(2008, 7,
8), "luoguanzhong");
Book b3 = new Book(10002, "shuifuzhuan", 100.8, new GregorianCalendar(2009, 6,
28), "shinaan ");
Book b4 = new Book(10003, "xiyouji", 120.8, new GregorianCalendar(2011, 6,
8), "wuchengen");
Book b5 = new Book(10004, "tian long ba bu", 10.4, new GregorianCalendar(2011, 9,
23), "souhu");
list.add(b1);
list.add(b2);
list.add(b3);
list.add(b4);
list.add(b5);
System.out.println("elements in array list:");
myprint(list); // use the default sorting in Book(the sorting method is compareTo()).
// in myprint(), it gets string by Book.toString().
Collections.sort(list, new PriceComparator()); // sorting according to price
System.out.println("sorting according to price:");
myprint(list);
Collections.sort(list, new CalendarComparator()); // sorting according to publishing date
System.out.println("sorting according to publishing date:");
myprint(list);
}
// customized method: print all elements in array list, each line one element
public static void myprint(List<Book> list) {
Iterator it = list.iterator(); // get iterator to work all elements in array list
while (it.hasNext()) { // if there are elements in iterator, return true
System.out.println("\t" + it.next());// print this element
}
}
// customized comparator: sorting by book price
static class PriceComparator implements Comparator {
public int compare(Object object1, Object object2) {// implement method in interface
Book p1 = (Book) object1; // forced type conversion
Book p2 = (Book) object2;
return new Double(p1.price).compareTo(new Double(p2.price));
}
}
// customized comparator: sorting according to publishing date
static class CalendarComparator implements Comparator {
public int compare(Object object1, Object object2) {// implement method in interface
Book p1 = (Book) object1; // forced type conversion
Book p2 = (Book) object2;
return p2.calendar.compareTo(p1.calendar);
}
}
}
the result of Book.java:
10000 hong lou meng 150.86 cao xueqin, gao e 2009.02.25 kucun:255
10001 san guo yan yi 99.68 luo guanzhong 2008.08.08 kucun:122
10002 shui fu zhuan 100.80 shi na an 2009.07.28 kucun:688
10003 xi you ji 120.80 wu cheng en 2011.07.08 kucun:453
10004 tian long ba bu 10.40 sou hu 2011.10.23 kucun:40
the result of UseComparator.java:
elements in array list:
10000 hongloumeng 150.86 caoxueqin, gaoe 2009.02.25
10001 sanguoyanyi 99.68 luoguanzhong 2008.08.08
10002 shuifuzhuan 100.80 shinaan 2009.07.28
10003 xiyouji 120.80 wuchengen 2011.07.08
10004 tian long ba bu 10.40 souhu 2011.10.23
sorting according to price:
10004 tian long ba bu 10.40 souhu 2011.10.23
10001 sanguoyanyi 99.68 luoguanzhong 2008.08.08
10002 shuifuzhuan 100.80 shinaan 2009.07.28
10003 xiyouji 120.80 wuchengen 2011.07.08
10000 hongloumeng 150.86 caoxueqin, gaoe 2009.02.25
sorting according to publishing date:
10004 tian long ba bu 10.40 souhu 2011.10.23
10003 xiyouji 120.80 wuchengen 2011.07.08
10002 shuifuzhuan 100.80 shinaan 2009.07.28
10000 hongloumeng 150.86 caoxueqin, gaoe 2009.02.25
10001 sanguoyanyi 99.68 luoguanzhong 2008.08.08