NSSortDescriptor可以根据数组中对象的属性来排序 为排序数组的每个属性创建NSSortDescriptor对象,将所有这些对象放入一个数组中,该数组将会在后面用作参数。 使用NSArray类的sortedArrayUsingDescripors:方法并将NSSortDescriptor对象数组作为参数传递过去,会返回一个排好序的数组 新建一个Peron类 #import initWithName:@"ee" age:5]; NSArray *personArr = @[p1,p2,p3,p4,p5]; // 为每个属性创建NSSortDescriptor 对象 NSSortDescriptor *sdAge = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES]; NSSortDescriptor *sdName = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
NSDescriptor排序(NSSet,NSArray,NSMutableArray) 单关键字排序 NSMutableArray *array = [NSMutableArray array]; NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"key" ascending:YES]; [array sortUsingDescriptors:[ NSSortDescriptor *sort1 = [NSSortDescriptor sortDescriptorWithKey:@"key1" ascending:YES]; NSSortDescriptor *sort2 = [NSSortDescriptor sortDescriptorWithKey:@"key2" ascending:NO]; [array sortUsingDescriptors
, //sortDescriptorWithKey 参数要的就是你对象中,要依据哪个属性来排序,你就把哪个属性的名字当成key传入 //ascending YES表示正序 NO表示倒叙 NSSortDescriptor * d1 = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:NO]; NSSortDescriptor * d2 = [NSSortDescriptor sortDescriptorWithKey:@"year" ascending:NO]; //如果你要使用多个属性进行排序,默认在前面的NSSortDescriptor优先级比较高 NSArray
otherObject.birthDate];}NSArray*sortedArray; sortedArray =[drinkDetails sortedArrayUsingSelector:@selector(compare:)]; NSSortDescriptor (better) or usually even better: NSSortDescriptor*sortDescriptor; sortDescriptor =[[[NSSortDescriptor
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Student"]; //实例化排序对象 NSSortDescriptor *ageSort = [NSSortDescriptor sortDescriptorWithKey:@"age"ascending:YES]; NSSortDescriptor *numberSort = [NSSortDescriptor sortDescriptorWithKey:@"number"ascending:YES]; request.sortDescriptors = @[ageSort
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Student"]; //实例化排序对象 NSSortDescriptor *ageSort = [NSSortDescriptor sortDescriptorWithKey:@"age"ascending:YES]; NSSortDescriptor *numberSort = [NSSortDescriptor sortDescriptorWithKey:@"number"ascending:YES]; request.sortDescriptors = @[ageSort
@FetchRequest( sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp, ascending: true)] 上面的代码对应的 NSFetchRequest 如下: @FetchRequest( sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp let fetchRequest = NSFetchRequest<Item>(entityName: "Item") fetchRequest.sortDescriptors = [NSSortDescriptor ListEachRowHasID 进行如下修改: struct ListEachRowHasID: View { @FetchRequest( sortDescriptors: [NSSortDescriptor @FetchRequest( sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp, ascending: true)]
AFCachedImage*> *sortedImages = [NSMutableArray arrayWithArray:self.cachedImages.allValues]; NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"lastAccessDate" AFCachedImage*> *sortedImages = [NSMutableArray arrayWithArray:self.cachedImages.allValues]; NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"lastAccessDate"
NSSortDescriptor 的改动: 现在,在 Swift Foundation 中包含了 NSSortDescriptor 的实现,同时它与 Objective-C 中对应部分兼容。 因为有部分的 API 改动,所以推荐有用到 NSSortDescriptor 的开发者看一下对应的文档。
kUserEntityName]; // 返回数据的排序规则(最少需要有一个sort descriptor) // Need at least one sort descriptor NSSortDescriptor *nameSort = [NSSortDescriptor sortDescriptorWithKey:kUserNameKey ascending:YES]; [request setSortDescriptors
allPhotosOptions = [[PHFetchOptions alloc] init]; allPhotosOptions.sortDescriptors = @[[NSSortDescriptor
NSFetchRequest fetchRequestWithEntityName:@"SchoolClass"]; //设置数据排序 [request setSortDescriptors:@[[NSSortDescriptor
- (void)sizeToFit; //提供了这个属性,会在列标题那里显示一个排序按钮 点击列标题后可以进行排序操作(会回调相关协议方法) @property (nullable, copy) NSSortDescriptor ; column2.headerToolTip = @"提示"; column2.hidden=NO; column2.sortDescriptorPrototype = [NSSortDescriptor return 30; } //排序回调函数 -(void)tableView:(NSTableView *)tableView sortDescriptorsDidChange:(NSArray<NSSortDescriptor ; column2.headerToolTip = @"提示"; column2.hidden=NO; column2.sortDescriptorPrototype = [NSSortDescriptor * 当用户修改了行排序规则时调用的回调 */ - (void)tableView:(NSTableView *)tableView sortDescriptorsDidChange:(NSArray<NSSortDescriptor
//查询有无对应的date 有则使用无则创建 NSFetchRequest *fdate = [TallyDate fetchRequest]; NSArray<NSSortDescriptor *> *sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"date" ascending:YES]]; fdate.sortDescriptors @"TallyDate" inManagedObjectContext:managedObjectContext]; [fetchRequest setEntity:entity]; NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:NO]; [fetchRequest setSortDescriptors *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"timestamp" ascending:NO]; [fetchRequest2
NSString *key, id value) { NSMutableArray *mutableQueryStringComponents = [NSMutableArray array]; NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"description" ascending:YES selector:@selector
PHFetchOptions *allPhotoOptions = [[PHFetchOptions alloc] init]; allPhotoOptions.sortDescriptors = @[[NSSortDescriptor PHFetchOptions *allPhotoOptions = [[PHFetchOptions alloc] init]; allPhotoOptions.sortDescriptors = @[[NSSortDescriptor
Environment(\.managedObjectContext) private var viewContext @FetchRequest( sortDescriptors: [NSSortDescriptor Environment(\.managedObjectContext) private var viewContext @FetchRequest( sortDescriptors: [NSSortDescriptor
NSSortDescriptor类实例。也是查询数据的时候会用到。 还可以通过NSSortDescriptor设置获取数据的排列顺序。 NSPredicate predicateWithFormat:@"firstName == %@", firstName]]; // 取回的数据按userID进行由小到大(升序)的排序 NSSortDescriptor *userIDSort = [NSSortDescriptor sortDescriptorWithKey:@"userID" ascending:YES]; // 注意,这个参数是一个数组
, nonatomic, strong) NSPredicate *predicate; //数据排序 @property (nullable, nonatomic, strong) NSArray<NSSortDescriptor
获取所有资源的集合,并按资源的创建时间排序 PHFetchOptions *options = [[PHFetchOptions alloc] init]; options.sortDescriptors = @[[NSSortDescriptor