KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > utils > IndexOfComparator


1 /*
2  * Created on 07.03.2005
3  */

4 package com.tonbeller.wcf.utils;
5
6 import java.util.Comparator JavaDoc;
7 import java.util.List JavaDoc;
8
9 /**
10  * comparator to sort a list in the order of another list. Missing elements are placed
11  * at the end.
12  *
13  * @author av
14  * @since 07.03.2005
15  */

16 public class IndexOfComparator implements Comparator JavaDoc {
17
18   List JavaDoc list;
19
20   public IndexOfComparator(List JavaDoc list) {
21     this.list = list;
22   }
23
24   public int compare(Object JavaDoc o1, Object JavaDoc o2) {
25     int i1 = list.indexOf(o1);
26     int i2 = list.indexOf(o2);
27     if (i1 < 0 && i2 < 0)
28       return 0;
29     if (i1 < 0)
30       return 1;
31     if (i2 < 0)
32       return -1;
33     if (i1 > i2)
34       return 1;
35     if (i1 < i2)
36       return -1;
37     return 0;
38   }
39 }
40
Popular Tags