【发布时间】:2026-01-11 21:00:01
【问题描述】:
我有一个具有多个属性的自定义对象。
一个属性是一个类型,例如一个枚举:A、B、C 等
这些也都有一个日期。
@property (assign) CustomType customType;
@property (nonatomic, strong) NSDate *startDate;
...
typedef NS_ENUM(NSInteger, CustomType) {
A,
B,
C
};
我想先放置所有类型为“C”的对象,然后按日期 DESC 对其余对象进行排序。
实现这一目标的最佳方法是什么?
我尝试添加多个 sortDescriptor,但我只希望 C 对象位于顶部,然后其余按日期排序,这将按日期对每个分组进行排序。
NSSortDescriptor *sortDescriptor1 =[[NSSortDescriptor alloc] initWithKey:@"customType" ascending:NO];
NSSortDescriptor *sortDescriptor2 =[[NSSortDescriptor alloc] initWithKey:@"startDate" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1, sortDescriptor2, nil];
NSArray *sortedObjects = [_items sortedArrayUsingDescriptors:sortDescriptors];
例子
+-------------------------+
| C | 2017-08-16 12:48:53 |
| C | 2017-08-15 12:46:53 |
| B | 2017-08-16 12:48:53 |
| B | 2017-08-14 12:43:53 |
| A | 2017-08-15 12:46:53 |
| A | 2017-08-14 12:31:53 |
+-------------------------+
想要
+-------------------------+
| C | 2017-08-16 12:48:53 |
| C | 2017-08-15 12:46:53 |
| A | 2017-08-16 12:48:53 |
| B | 2017-08-15 12:46:53 |
| A | 2017-08-14 12:43:53 |
| B | 2017-08-14 12:31:53 |
+-------------------------+
我还可以决定我首先想要哪个 customType,所以我可能希望 B 位于顶部,然后其余的只是按日期?
将数组分成两组是否更容易,ALL C 然后其余的。对每个子组进行排序,然后将它们重新组合在一起?
然后我可以在下面的谓词中选择我想要的最高组:
NSPredicate *pred1 = [NSPredicate predicateWithFormat:@"customType == %ld", C];
NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"customType != %ld", C];
NSArray *top = [_items filteredArrayUsingPredicate:pred1];
NSArray *bottom = [_items filteredArrayUsingPredicate:pred2];
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"startDate" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1, nil];
NSArray *sortedTop = [top sortedArrayUsingDescriptors:sortDescriptors];
NSArray *sortedBottom = [bottom sortedArrayUsingDescriptors:sortDescriptors];
//NSArray *items = [NSArray arrayWithObjects:sortedTop, sortedBottom, nil];
NSArray *newArray = @[];
newArray = [newArray arrayByAddingObjectsFromArray:sortedTop];
newArray = [newArray arrayByAddingObjectsFromArray:sortedBottom];
_items = [newArray mutableCopy];
按日期排序 https://*.com/a/6084944/2895831
NSLog(@"nodeEventArray == %@", nodeEventArray);
NSSortDescriptor *dateDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"startDate" ascending:NO];
NSArray *sortDescriptors = [NSArray arrayWithObject:dateDescriptor];
NSArray *sortedEventArray = [nodeEventArray sortedArrayUsingDescriptors:sortDescriptors];
NSLog(@"sortedEventArray == %@", sortedEventArray);
过去,我按给定属性对数组进行分组,并对该单个属性使用自定义排序。
How to use Custom ordering on NSArray
@property (nonatomic, strong) NSString *level;
NSArray *levels = [array valueForKeyPath:@"@distinctUnionOfObjects.level"];
NSArray *levelsSortOrder = @[@"Beginner", @"Basic", @"Intermediate", @"Expert", @"Advanced", @"Developer", @"Seminar"];
NSArray *sortedArray = [levels sortedArrayUsingComparator: ^(id obj1, id obj2){
NSUInteger index1 = [levelsSortOrder indexOfObject: obj1];
NSUInteger index2 = [levelsSortOrder indexOfObject: obj2];
NSComparisonResult ret = NSOrderedSame;
if (index1 < index2)
{
ret = NSOrderedAscending;
}
else if (index1 > index2)
{
ret = NSOrderedDescending;
}
return ret;
}];
【问题讨论】:
标签: ios objective-c sorting nsarray