如何使用NSSortDescriptor对NSMutableArray进行排序

4

我正在使用以下NSSortDescriptor代码来对数组进行排序。目前我是按照价格排序,但希望也能限制价格。例如,是否可以按价格排序但只显示小于100的价格?

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
                                        initWithKey: @"price" ascending: YES];

NSMutableArray *sortedArray = (NSMutableArray *)[self.displayItems
                                                     sortedArrayUsingDescriptors: [NSArray arrayWithObject:sortDescriptor]];

[self setDisplayItems:sortedArray];

[self.tableView reloadData];

你不能直接将 NSArray 强制转换为 NSMutableArray。请使用 [array mutableCopy] - Sebastian
Sebastian是正确的。一个NSArray只要存在,就会一直是一个NSArray,除非它是子类的成员,在这种情况下,它将始终是该子类的成员。 - user749127
4个回答

14

仅对数组进行排序是不够的 - 您还需要对其进行过滤。

如果保持原始代码的结构,您可以像这样添加一个过滤器:

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
                                    initWithKey: @"price" ascending: YES];

NSArray *sortedArray = [self.displayItems sortedArrayUsingDescriptors: [NSArray arrayWithObject:sortDescriptor]];

NSPredicate *pred = [NSPredicate predicateWithFormat: @"price < 100"];
NSMutableArray *filteredAndSortedArray = [sortedArray filteredArrayUsingPredicate: pred];

[self setDisplayItems: [filteredAndSortedArray mutableCopy]];

[self.tableView reloadData];

如果性能成为问题,您可能想要反转过滤和排序的顺序,但这只是细节。


1
您可以先使用指定价格范围过滤数组,然后对过滤后的数组进行排序并在表视图中显示已排序的数组!!!
对于过滤,您可以使用NSPredicate,对于排序,您可以使用相同的NSSortDescriptor 希望这有所帮助!!!

0
NSSortDescriptor *sorter = [[NSSortDescriptor alloc] initWithKey:@"your key" ascending:true];
[yourarray sortUsingDescriptors:[NSArray arrayWithObject:sorter]];
[sorter release];

谢谢,但这不是我已经有的吗?我需要不仅按价格(我的键)排序,而且只显示价格如果它<= 100等... - hanumanDev
为什么要踩?NSSortDescriptor只是用于对数组进行排序。如果您想设置价格限制,请使用NSPredicate。 - Girish
@Sebastian 根据问题,我已经给出了使用NSSortDescriptor对数组进行排序的答案。 - Girish
2
但问题是:是否可以按价格排序,但仅显示小于100的价格? - Sebastian

0
NSMutableArray * weekDays = [[NSMutableArray alloc] initWithObjects:@"Sunday",@"Monday",@"Tuesday",@"Wednesday",@"Thursday",@"Friday",@"Saturday", nil];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
NSMutableArray *dictArray = [[NSMutableArray alloc] init];

for(int i = 0; i < [weekDays count]; i++)
{
    dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%i",i],@"WeekDay",[weekDays objectAtIndex:i],@"Name",nil];
    [dictArray addObject:dict];
}
NSLog(@"Before Sorting : %@",dictArray);

@try
{
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Name" ascending:NO];
    NSArray *descriptor = @[sortDescriptor];
    NSArray *sortedArray = [dictArray sortedArrayUsingDescriptors:descriptor];
    NSLog(@"After Sorting : %@",sortedArray);
}
@catch (NSException *exception)
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sorting cant be done because of some error" message:[NSString stringWithFormat:@"%@",exception] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
    [alert setTag:500];
    [alert show];
    [alert release];
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接