KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > pojos > WeblogEntryComparator


1 /*
2  * Created on Apr 16, 2004
3  */

4 package org.roller.pojos;
5
6 import java.io.Serializable JavaDoc;
7 import java.util.Comparator JavaDoc;
8
9 /**
10  * Sorts WeblogEntryData objects in reverse chronological order
11  * (most recently published entries first). If they happen to
12  * have the same pubTime, then sort alphabetically by title.
13  *
14  * @author lance.lavandowska
15  */

16 public class WeblogEntryComparator implements Comparator JavaDoc, Serializable JavaDoc
17 {
18     static final long serialVersionUID = -9067148992322255150L;
19     
20     public int compare(Object JavaDoc val1, Object JavaDoc val2)
21     throws ClassCastException JavaDoc
22     {
23         WeblogEntryData entry1 = (WeblogEntryData)val1;
24         WeblogEntryData entry2 = (WeblogEntryData)val2;
25         long pubTime1 = entry1.getPubTime().getTime();
26         long pubTime2 = entry2.getPubTime().getTime();
27
28         if (pubTime1 > pubTime2)
29         {
30             return -1;
31         }
32         else if (pubTime1 < pubTime2)
33         {
34             return 1;
35         }
36
37         // if pubTimes are the same, return
38
// results of String.compareTo() on Title
39
return entry1.getTitle().compareTo(entry2.getTitle());
40     }
41 }
42
Popular Tags