如何build立二级NSSortDescriptorsorting键?
我已经成功地sorting我的sorting键我的lastName
,但我想知道如何按lastName
sorting,然后通过firstName
。 这里是我用lastName
sorting的代码
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"firstName" ascending:YES]; [request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
如何添加firstName
的二级sorting键?
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"firstName" ascending:YES]; NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"lastName" ascending:YES]; [request setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor1, sortDescriptor2, nil]];
注意你正在传递一个sorting描述符数组。 只需为firstname创build另一个描述符,并使用两个描述符创build数组。 它们将按照数组的顺序应用。