KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > snip > PostNameComparator


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25
26 package org.snipsnap.snip;
27
28 import java.util.Comparator JavaDoc;
29
30 /**
31  * Compares names like 2003-10-11/3 and sorts them
32  * in reverse order
33  *
34  * 2003-10-05/11 2003-10-06/1 2003-10-05/1 2003-10-05/2
35  *
36  * is sorted to:
37  *
38  * 2003-10-06/1 2003-10-05/11 2003-10-05/2 2003-10-05/1
39  *
40  * @author stephan
41  * @version $Id: PostNameComparator.java 1606 2004-05-17 10:56:18Z leo $
42  */

43
44 public class PostNameComparator implements Comparator JavaDoc {
45   public int compare(Object JavaDoc o1, Object JavaDoc o2) {
46     if (! (o1 instanceof String JavaDoc) || !( o2 instanceof String JavaDoc)) {
47       throw new ClassCastException JavaDoc();
48     }
49     String JavaDoc name1 = (String JavaDoc) o1;
50     String JavaDoc name2 = (String JavaDoc) o2;
51     int index1 = name1.lastIndexOf("/");
52     int index2 = name2.lastIndexOf("/");
53     int result = name2.substring(0, index2 != -1 ? index2 : name2.length())
54       .compareTo(name1.substring(0, index1 != -1 ? index1 : name1.length()));
55     if (0 == result) {
56       int number1 = Integer.parseInt(name1.substring(index1+1));
57       int number2 = Integer.parseInt(name2.substring(index2+1));
58       result = number1 > number2 ? -1 : 1;
59     }
60     return result;
61   }
62 }
63
Popular Tags