BDSortOrdering

Inherits from:
NSObject
Conforms to:
NSCoding
Declared in:
BDControl/BDSortOrdering.h

Class at a Glance

A BDSortOrdering specifies a way to sort objects.


Class Description

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.

NSObject (BDSortOrderingAdditions)

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.

NSArray (BDSortOrderingAdditions)

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.

NSMutableArray (BDSortOrderingAdditions)

A BDSortOrderingAdditions category on NSMutableArray adds the method -sortUsingKeyOrderArray:. This is similar to the -sortedArrayUsingKeyOrderArray:, except it sorts the receiver in-place.

Example

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];
}

Adopted Protocols

NSCoding
-encodeWithCoder:
-initWithCoder:

Method Types

Initialization
-initWithKey:selector:
+sortOrderingWithKey:selector:
 
Accessors
-key
-selector

Class Methods

sortOrderingWithKey:selector:

+ (BDSortOrdering *)sortOrderingWithKey:(NSString *)key selector:(SEL)selector

Returns a new, autoreleased BDSortOrdering instance initialized with the given key and selector.


Instance Methods

initWithKey:selector:

- (id)initWithKey:(NSString *)key selector:(SEL)selector

Initializes an instance of BDSortOrdering to sort on the given key using the comparison selector selector.

key

- (NSString *)key

Returns the key used to sort on.

selector

- (SEL)selector

Returns the comparison selector used to sort.


Copyright © 2002, 2003, bDistributed.com, Inc. All rights reserved worldwide.