Skip to content

Most visited

Recently visited

navigation
Added in API level 1

RuleBasedCollator

public class RuleBasedCollator
extends Collator

java.lang.Object
   ↳ java.text.Collator
     ↳ java.text.RuleBasedCollator


A concrete subclass of Collator. It is based on the ICU RuleBasedCollator which implements the CLDR and Unicode collation algorithms.

Most of the time, you create a Collator instance for a Locale by calling the getInstance() factory method. You can construct a RuleBasedCollator if you need a custom sort order.

The root collator's sort order is the CLDR root collation order which in turn is the Unicode default sort order with a few modifications. A RuleBasedCollator is built from a rule String which changes the sort order of some characters and strings relative to the default order.

A rule string usually contains one or more rule chains. A rule chain consists of a reset followed by one or more rules. The reset anchors the following rules in the default sort order. The rules change the order of the their characters and strings relative to the reset point.

A reset is an ampersand & followed by one or more characters for the reset position. A rule is a relation operator, which specifies the level of difference, also followed by one or more characters. A multi-character rule creates a "contraction". A multi-character reset position usually creates "expansions".

For example, the following rules make "ä" sort with a diacritic-like (secondary) difference from "ae" (like in German phonebook sorting), and make "å" and "aa" sort as a base letter (primary) after "z" (like in Danish). Uppercase forms sort with a case-like (tertiary) difference after their lowercase forms.

 &AE<<ä <<<Ä
 &z<å<<<Å<<<aa<<<Aa<<<AA
 

For details see

Note: earlier versions of RuleBasedCollator up to and including Android 4.4 (KitKat) allowed the omission of the reset from the first rule chain. This was interpreted as an implied reset after the last non-Han script in the default order. However, this is not a useful reset position, except for large tailorings of Han characters themselves. Starting with the CLDR 24 collation specification and the ICU 53 implementation, the initial reset is required.

If the rule string does not follow the syntax, then RuleBasedCollator throws a ParseException.

Summary

Inherited constants

From class java.text.Collator

Public constructors

RuleBasedCollator(String rules)

Constructs a new instance of RuleBasedCollator using the specified rules.

Public methods

Object clone()

Returns a new collator with the same collation rules, decomposition mode and strength value as this collator.

int compare(String source, String target)

Compares the source text to the target text according to the collation rules, strength and decomposition mode for this RuleBasedCollator.

boolean equals(Object obj)

Compares the specified object with this RuleBasedCollator and indicates if they are equal.

CollationElementIterator getCollationElementIterator(String source)

Obtains a CollationElementIterator for the given string.

CollationElementIterator getCollationElementIterator(CharacterIterator source)

Obtains a CollationElementIterator for the given CharacterIterator.

CollationKey getCollationKey(String source)

Returns the CollationKey for the given source text.

String getRules()

Returns the collation rules of this collator.

int hashCode()

Returns an integer hash code for this object.

Inherited methods

From class java.text.Collator
From class java.lang.Object
From interface java.util.Comparator

Public constructors

RuleBasedCollator

Added in API level 1
RuleBasedCollator (String rules)

Constructs a new instance of RuleBasedCollator using the specified rules. (See the class description.)

Note that the rules are interpreted as a delta to the default sort order. This differs from other implementations which work with full rules specifications and may result in different behavior.

Parameters
rules String: the collation rules.
Throws
NullPointerException if rules == null.
ParseException if rules contains rules with invalid collation rule syntax.

Public methods

clone

Added in API level 1
Object clone ()

Returns a new collator with the same collation rules, decomposition mode and strength value as this collator.

Returns
Object a shallow copy of this collator.

See also:

compare

Added in API level 1
int compare (String source, 
                String target)

Compares the source text to the target text according to the collation rules, strength and decomposition mode for this RuleBasedCollator. See the Collator class description for an example of use.

Parameters
source String: the source text.
target String: the target text.
Returns
int an integer which may be a negative value, zero, or else a positive value depending on whether source is less than, equivalent to, or greater than target.

equals

Added in API level 1
boolean equals (Object obj)

Compares the specified object with this RuleBasedCollator and indicates if they are equal. In order to be equal, object must be an instance of Collator with the same collation rules and the same attributes.

Parameters
obj Object: the object to compare with this object.
Returns
boolean true if the specified object is equal to this RuleBasedCollator; false otherwise.

See also:

getCollationElementIterator

Added in API level 1
CollationElementIterator getCollationElementIterator (String source)

Obtains a CollationElementIterator for the given string.

Parameters
source String: the source string.
Returns
CollationElementIterator the CollationElementIterator for source.

getCollationElementIterator

Added in API level 1
CollationElementIterator getCollationElementIterator (CharacterIterator source)

Obtains a CollationElementIterator for the given CharacterIterator. The source iterator's integrity will be preserved since a new copy will be created for use.

Parameters
source CharacterIterator: the source character iterator.
Returns
CollationElementIterator a CollationElementIterator for source.

getCollationKey

Added in API level 1
CollationKey getCollationKey (String source)

Returns the CollationKey for the given source text.

Parameters
source String: the specified source text.
Returns
CollationKey the CollationKey for the given source text.

getRules

Added in API level 1
String getRules ()

Returns the collation rules of this collator. These rules can be fed into the RuleBasedCollator(String) constructor.

The returned string will be empty unless you constructed the instance yourself. The string forms of the collation rules are omitted to save space on the device.

Returns
String

hashCode

Added in API level 1
int hashCode ()

Returns an integer hash code for this object. By contract, any two objects for which equals(Object) returns true must return the same hash code value. This means that subclasses of Object usually override both methods or neither method.

Note that hash values must not change over time unless information used in equals comparisons also changes.

See Writing a correct hashCode method if you intend implementing your own hashCode method.

Returns
int this object's hash code.
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.