我有一个自定义对象,例如:
#import <Foundation/Foundation.h>
@interface Store : NSObject{
NSString *name;
NSString *address;
}
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *address;
@end
我有一个包含 Store 对象的 NSMutableArray (storeArray) 数组:
store1 = [[Store alloc] init];
store1.name = @"Walmart";
store1.address = @"walmart address here..";
store2 = [[Store alloc] init];
store2.name = @"Target";
store2.address = @"Target address here..";
store3 = [[Store alloc] init];
store3.name = @"Apple Store";
store3.address = @"Apple store address here..";
//add stores to array
storeArray = [[NSMutableArray alloc] init];
[storeArray addObject:store1];
[storeArray addObject:store2];
[storeArray addObject:store3];
我的问题是如何按商店名称对数组进行排序?我知道我可以使用这一行按字母顺序对数组进行排序:
[nameOfArray sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
如何将其应用于我的 Store 类的商店名称?
NSSortDescriptor *sortDescriptor =
[NSSortDescriptor sortDescriptorWithKey:@"name"
ascending:YES
selector:@selector(caseInsensitiveCompare:)];
[nameOfArray sortedArrayUsingDescriptors:@[sortDescriptor]];
相关文档:
- [NSArray SortedArrayUsingDescriptors:] http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html#//apple_ref/occ/instm/NSArray/sortedArrayUsingDescriptors%3a
- NSSortDescriptor http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSSortDescriptor_Class/Reference/Reference.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)