PATH |
NSComparator
- Inherits from:
- Object
- Package:
- com.webobjects.foundation
Class Description
NSComparator is an abstract class that defines an API for comparing two objects for the purpose of sorting them. The class defines one method, compare, which compares two parameters and returns one of OrderedAscending
, OrderedSame, or OrderedDescending.
Instead of invoking compare directly on a comparator, you typically use the NSArray method sortedArrayUsingComparator, which sorts the elements of the receiving array into a new array, or the NSMutableArray method sortUsingComparator, which sorts the elements of an array in place. NSComparator provides default comparators to use with these sorting methods. See the section "Constants" (page 54).
Constants
NSComparator defines the following int
constants as the possible return values for compare:
Additionally, NSComparator defines the following NSComparator constants to be used for comparing objects of the specified class:
Constant | Compares Objects of Class |
AscendingStringComparator | String |
DescendingStringComparator | String |
AscendingCaseInsensitiveStringComparator | String |
DescendingCaseInsensitiveStringComparator | String |
AscendingNumberComparator | Number |
DescendingNumberComparator | Number |
AscendingTimestampComparator | NSTimestamp |
DescendingTimestampComparator | NSTimestamp |
Constructors
NSComparator
public NSComparator()
Instance Methods
compare
public abstract int compare( Object first, Object second) throws NSComparator.ComparisonException
compare(x, x)
returnsOrderedSame
.- If
compare(x, y)
returnsOrderedSame
, thencompare(y, x)
returnsOrderedSame
- If
compare(x, y)
returnsOrderedAscending
, thencompare(y, x)
returnsOrderedDescending
. - If
compare(x, y)
returnsOrderedDescending
, thencompare(y, x)
returnsOrderedAscending
. - If
compare(x, y)
returnsOrderedAscending
andcompare(y, z)
returnsOrderedAscending
, thencompare(x, z)
returnsOrderedAscending
. - Exactly one of the following is true:
compare(x, x)
==OrderedSame
,compare(x, x)
==OrderedAscending
, orcompare(x, x)
==OrderedDescending
. - The result of
compare(x, y)
must be the same in all invocations.
Throws an NSComparator.ComparisonException if a comparison between first and second is impossible or undefined; for example, if either argument is null
.
© 2001 Apple Computer, Inc. (Last Published April 17, 2001)