PATH |
- Inherits from:
- Object
- Package:
- com.webobjects.foundation
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).
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 |
public NSComparator()
public abstract int compare( Object first, Object second) throws NSComparator.ComparisonException
compare(x, x)
returns OrderedSame
.
compare(x, y)
returns OrderedSame
, then compare(y, x)
returns OrderedSame
compare(x, y)
returns OrderedAscending
, then compare(y, x)
returns OrderedDescending
.
compare(x, y)
returns OrderedDescending
, then compare(y, x)
returns OrderedAscending
.
compare(x, y)
returns OrderedAscending
and compare(y, z)
returns OrderedAscending
, then compare(x, z)
returns OrderedAscending
.
compare(x, x)
== OrderedSame
, compare(x, x)
== OrderedAscending
, or compare(x, x)
== OrderedDescending
.
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)