简体   繁体   English

以特定顺序对 NSArray 的 NSString 进行排序

[英]ordering NSString of NSArray in a particular order

I have a NSString in an NSArray and I wanted to order this string/fields based on how important it is.我在 NSArray 中有一个 NSString,我想根据它的重要性来排序这个字符串/字段。 So say the string is B, H, A, Q, Z, L, M, O.所以说字符串是 B、H、A、Q、Z、L、M、O。

I wanted it to be always sorted as A, Q, Z, B, H, O, L, M. This is a predefined set of rule.我希望它始终按 A、Q、Z、B、H、O、L、M 排序。这是一组预定义的规则。 How do I do this?我该怎么做呢? Can this be done using NSSortDescriptor?这可以使用 NSSortDescriptor 来完成吗?

The short answer: Yes!简短的回答:是的!

And here's how...这是如何...

Since there are two pieces of information you need to know about your value (the importance and the value itself), you should create an object with these two important pieces of information, then store in an array similar to the way you store your strings.由于您需要了解关于您的价值的两条信息(重要性和价值本身),因此您应该使用这两条重要信息创建一个对象,然后将其存储在类似于存储字符串的方式的数组中。 This makes it simple if, say, you want to change the 'importance' some time later with very little effort:如果你想在一段时间后不费吹灰之力地改变“重要性”,这就很简单了:

@interface MyObject : NSObject
@property (nonatomic, readwrite) NSInteger sortOrder;
@property (nonatomic, retain) NSString *value;
@end

@implementation MyObject
@synthesize sortOrder;
@synthesize value;
-(NSString *)description
{
   //...so I can see the values in console should I NSLog() it
   return [NSString stringWithFormat:@"sortOrder=%i, value=%@", self.sortOrder, self.value];
}
-(void)dealloc
{
    self.value = nil;
    [super dealloc];
}
@end

Add your objects to an array.将您的对象添加到数组中。 Then sort:然后排序:

NSMutableArray *myArrayOfObjects = [NSMutableArray array];

//Add your objects
MyObject *obj = [[[MyObject alloc] init] autorelease];
obj.sortOrder = 1;
obj.value = @"A";
[myArrayOfObjects addObject:obj];

obj = [[[MyObject alloc] init] autorelease];
obj.sortOrder = 2;
obj.value = @"Q";
[myArrayOfObjects addObject:obj];

//Sort the objects according to importance (sortOrder in this case)
NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"sortOrder" ascending:YES] autorelease];
NSArray *sortedArray = [myArrayOfObjects sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
NSLog(sortedArray);  //<--See for yourself that they are sorted

NSArray has several sort functions. NSArray 有几个排序函数。 Three you might consider are:您可能会考虑的三个是:

- (NSArray *)sortedArrayUsingComparator:(NSComparator)cmptr

- (NSArray *)sortedArrayUsingSelector:(SEL)comparator

- (NSArray *)sortedArrayUsingFunction:(NSInteger (*)(id, id, void *))comparator context:(void *)context

I think you might find the second, selector-based comparator the easiest to use to get started.我认为您可能会发现第二个基于选择器的比较器最容易开始使用。 See the docs here:请参阅此处的文档:

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html#//apple_ref/occ/instm/NSArray/sortedArrayUsingSelector : https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html#//apple_ref/occ/instm/NSArray/sortedArrayUsingSelector

EDIT:编辑:

I think using NSSortDescriptor may be overkill, but here is a good post describing it:我认为使用 NSSortDescriptor 可能有点矫枉过正,但这里有一篇很好的文章描述了它:

How to sort NSMutableArray using sortedArrayUsingDescriptors? 如何使用 sortedArrayUsingDescriptors 对 NSMutableArray 进行排序?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM