- Inherits from:
- NSObject
- Conforms to:
- NSCoding
- Declared in:
- BDControl/BDSortOrdering.h
A BDSortOrdering specifies a way to sort objects.
Instances of the BDSortOrdering class specify a way to sort arbitrary objects in a collection. A sort ordering contains the key on which the objects are to be sorted, and a selector that is used to compare objects as they're being sorted.
There are four default sort ordering selectors provided via a BDSortOrderingAdditions category on NSObject, and defined to convenience constants:
The first two selectors are defined for any class that responds to the compare: selector. The second two selectors are defined only for classes that respond to the caseInsensitiveCompare: selector, which in FoundationKit only includes NSString.
A BDSortOrderingAdditions category on NSArray adds the method -sortedArrayUsingKeyOrderArray:
. This method is passed an array of BDSortOrdering objects ranked in order from most to least specific. It returns a sorted array, since NSArray instances are immutable.
A BDSortOrderingAdditions category on NSMutableArray adds the method -sortUsingKeyOrderArray:
. This is similar to the -sortedArrayUsingKeyOrderArray:
, except it sorts the receiver in-place.
Given an array of Person objects with lastName and firstName properties corresponding to a person's last name and first name, to sort this array by last name and then first name, one could write the following:
- (NSArray *)sortPeopleByLastNameThenFirstName:(NSArray *)people
{
BDSortOrdering *lastNameOrdering, *firstNameOrdering;
NSArray *orderings;
lastNameOrdering = [BDSortOrdering sortOrderingWithKey:@"lastName"
selector:BDCompareCaseInsensitiveAscending];
firstNameOrdering = [BDSortOrdering sortOrderingWithKey:@"firstName"
selector:BDCompareCaseInsensitiveAscending];
orderings = [NSArray arrayWithObjects:lastNameOrdering, firstNameOrdering, nil];
return [people sortedArrayUsingKeyOrderArray:orderings];
}
- NSCoding
- -encodeWithCoder:
- -initWithCoder:
- Initialization
- -initWithKey:selector:
- +sortOrderingWithKey:selector:
- Accessors
- -key
- -selector
+ (BDSortOrdering *)sortOrderingWithKey:(NSString *)key selector:(SEL)selector
Returns a new, autoreleased BDSortOrdering instance initialized with the given key and selector.
- (id)initWithKey:(NSString *)key selector:(SEL)selector
Initializes an instance of BDSortOrdering to sort on the given key using the comparison selector selector.
- (NSString *)key
Returns the key used to sort on.
- (SEL)selector
Returns the comparison selector used to sort.