- All Known Implementing Classes:
- Collator, RuleBasedCollator
- See Also:
- Top Examples, Source Code,
Comparable ,
Arrays.sort(Object[], Comparator) ,
TreeMap ,
TreeSet ,
SortedMap ,
SortedSet ,
Serializable
public int compare(Object o1,
Object o2) - See Also:
- ClassCastException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[56]Sort vector with own comparator By Anonymous on 2005/03/16 19:04:12 Rate
import java.util.*; class Comparer implements Comparator { public int compare ( Object obj1, Object obj2 ) { int i1 = ( ( Integer ) obj1 ) .intValue ( ) ; int i2 = ( ( Integer ) obj2 ) .intValue ( ) ; return Math.abs ( i1 ) - Math.abs ( i2 ) ; } } public class collect { public static void main ( String args [ ] ) { Vector vec = new Vector ( ) ; vec.addElement ( new Integer ( -200 ) ) ; vec.addElement ( new Integer ( 100 ) ) ; vec.addElement ( new Integer ( 400 ) ) ; vec.addElement ( new Integer ( -300 ) ) ; Collections.sort ( vec ) ; for ( int i = 0; i < vec.size ( ) ; i++ ) { int e= ( ( Integer ) vec.elementAt ( i ) ) .intValue ( ) ; System.out.println ( e ) ; } Collections.sort ( vec, new Comparer ( ) ) ; for ( int i = 0; i < vec.size ( ) ; i++ ) { int e= ( ( Integer ) vec.elementAt ( i ) ) .intValue ( ) ; System.out.println ( e ) ; } } }
int compare(T o1,
T o2) - See Also:
- ClassCastException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[1570]A string Comparator By Anonymous on 2005/10/14 16:49:39 Rate
static final Comparator stringCmp = new Comparator ( ) { public int compare ( Object o1, Object o2 ) { String s1 = ( String ) o1; String s2 = ( String ) o2; int len1 = s1.length ( ) ; int len2 = s2.length ( ) ; for ( int i=0, n=Math.min ( len1, len2 ) ; i < n; i++ ) { char c1 = s1.charAt ( i ) ; char c2 = s2.charAt ( i ) ; if ( c1 != c2 ) return c1 - c2; } return len1 - len2; } } ; Collections.sort ( aList, stringCmp ) ; [1620]Case-insensitive sorting operation By Anonymous on 2005/11/04 20:23:08 Rate
//How to do a case-insensitive sorting operation for strings public class CaseInsensitiveComparator implements java.util.Comparator { public int compare ( Object o1, Object o2 ) { String s1 = o1.toString ( ) .toUpperCase ( ) ; String s2 = o2.toString ( ) .toUpperCase ( ) ; return s1.compareTo ( s2 ) ; } } // Needs to import java.util.*; Object [ ] data = { "Aubergine","banana","aubergine","Banana" } ; List list = Arrays.asList ( data ) ; Collections.sort ( list, new CaseInsensitiveComparator ( ) ) ; System.out.println ( list ) ; // Displays [ Aubergine, aubergine, banana, Banana ] [2011]util By vana on 2010/07/02 22:58:59 Rate
shine pattern has a util too, for using it http://sourceforge.net/projects/shine-enterpris/
boolean equals(Object obj) - See Also:
Object.hashCode()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[1795]Multibyte unicode lexical comparison By Anonymous on 2006/07/28 09:15:24 Rate
To perform multibyte unicode lexical comparisons instead of UTF-8 byte-by-byte comparisons, we can use String.compareTo ( ) , which performs a Unicode comparison of two strings. NOTE: that for single-byte roman characters, Unicode comparison and UTF-8 byte-by-byte comparisons are identical this is something you would only want to do if you were using multibyte unicode characters. import java.util.Comparator; public class MultibyteComparator implements Comparator { public MultibyteComparator ( ) { } public int compare ( Object d1, Object d2 ) { byte [ ] b1 = ( byte [ ] ) d1; byte [ ] b2 = ( byte [ ] ) d2; String s1 = new String ( b1, "UTF-8" ) ; String s2 = new String ( b2, "UTF-8" ) ; return s1.compareTo ( s2 ) ; } } | Popular Tags |