-4

我有一组自定义对象。对象包含状态:已付款或未付款。我想对数组进行排序:第一次单击时,对数组进行排序以先显示付费记录,然后再显示未付费记录。在第二次单击时,对数组进行排序以显示未付费记录,然后显示已付费记录。

有什么帮助吗?

4

2 回答 2

2

您可以尝试以下类似声明:

NSSortDescriptor *Sorter = [NSSortDescriptor sortDescriptorWithKey:@"yourfield" ascending:YES selector:@selector(caseInsensitiveCompare:)];
[yourArray sortUsingDescriptors:[NSArray arrayWithObject:Sorter]];
于 2013-01-25T05:56:48.983 回答
0

你可以这样做:

- (IBAction)sort:(id)sender {
    static BOOL isPaidFirst=YES;

    NSSortDescriptor *Sorter = [NSSortDescriptor sortDescriptorWithKey:@"isPaid"
                                                             ascending:!isPaidFirst
                                                              selector:@selector(compare:)];
    [self.array sortUsingDescriptors:[NSArray arrayWithObject:Sorter]];

    isPaidFirst=!isPaidFirst;


    for (MyObject *obj in self.array) {
        NSLog(@"-->%@, %ld, %d",obj.courseName, obj.totalFee, obj.isPaid);
    }
    NSLog(@"------------------");

}

这里 MyObject 是:

@interface MyObject : NSObject

@property(strong)NSString *courseName;
@property NSInteger totalFee;
@property BOOL isPaid;

@end
于 2013-01-25T06:54:33.030 回答