KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > sort > LowercaseFirstComparer


1 package com.icl.saxon.sort;
2
3 // Copyright © International Computers Limited 1998
4
// See conditions of use
5

6 /**
7  * A Comparer used for comparing keys
8  *
9  * @author Michael H. Kay (mhkay@iclway.co.uk)
10  *
11  */

12
13 public class LowercaseFirstComparer extends Comparer {
14
15     /**
16     * Compare two string objects: case is irrelevant, unless the strings are equal ignoring
17     * case, in which case lowercase comes first.
18     * @return <0 if a<b, 0 if a=b, >0 if a>b
19     * @throws ClassCastException if the objects are of the wrong type for this Comparer
20     */

21
22     public int compare(Object JavaDoc a, Object JavaDoc b) {
23         char[] a1 = ((String JavaDoc)a).toCharArray();
24         char[] b1 = ((String JavaDoc)b).toCharArray();
25         int alen = a1.length;
26         int blen = b1.length;
27         int i = 0;
28         int j = 0;
29
30         while (true) {
31             if (i==alen && j==blen) break;
32             if (i==alen) return -1;
33             if (j==blen) return +1;
34             int diff = Character.toLowerCase(a1[i]) -
35                              Character.toLowerCase(b1[j]);
36             i++;
37             j++;
38             if (diff!=0) return diff;
39         }
40
41         i = 0;
42         j = 0;
43         while (true) {
44             if (i==alen) return 0;
45             int diff = a1[i++] - b1[j++];
46             if (diff!=0) {
47                 return (Character.isLowerCase(a1[i-1]) ? -1 : +1);
48             }
49         }
50             
51     }
52
53
54 }
55
Popular Tags