KickJava   Java API By Example, From Geeks To Geeks.

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


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 UppercaseFirstComparer extends Comparer {
14
15     /**
16     * Compare two string objects: case is irrelevant, unless the strings are equal ignoring
17     * case, in which case uppercase 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         while (true) {
30             if (i==alen && j==blen) break;
31             if (i==alen) return -1;
32             if (j==blen) return +1;
33             int diff = Character.toLowerCase(a1[i++]) - Character.toLowerCase(b1[j++]);
34             if (diff!=0) return diff;
35         }
36         i = 0;
37         j = 0;
38         while (true) {
39             if (i==alen) return 0;
40             int diff = a1[i++] - b1[j++];
41             if (diff!=0) {
42                 return (Character.isUpperCase(a1[i-1]) ? -1 : +1);
43             }
44         }
45             
46     }
47         
48 }
49
Popular Tags