如何在ios中对多个NSArray升序和降序排序

时间:2015-06-11 13:31:11

标签: ios objective-c sorting

我有三个数组,priceArray,nameArray& discountArray。我在tableView中显示这些数组值。每个单元格都有价格,名称,折扣。

我想按价格低到高排序列表,反之亦然,nameArray和discountArray中的项目需要按照价格排序进行排序。

同样,我希望按照A-Z的名称排序,反之亦然,并相应地对价格和折扣进行排序。

1 cell -- 20,xxx,10%
2 cell -- 10,zzz,10%
3 cell -- 150,aaa,0%
4 cell -- 100,hhh,15%

By Price Low to High
   10,zzz,10%
   20,xxx,10%
   100,hhh,15%
   150,aaa,0%

By Name A-Z
   150,aaa,0%
   100,hhh,15%
   20,xxx,10%
   10,zzz,10%

帮我解决这个问题。

4 个答案:

答案 0 :(得分:8)

使用3个独立阵列无法满足您的要求。您需要将所有信息收集到模型对象中。

@interface Product:NSObject

@property NSString *name;
@property NSNumber *price;
@property NSNumber *discount;

@end

@implementation Product  
//this is empty 
@end

然后,您可以使用NSSortDescriptorProduct数组进行排序。

例如价格从低到高。

NSSortDescriptor *priceSortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"price" ascending:YES];
NSArray *sortedArray = [productArray sortedArrayUsingDescriptors:@[priceSortDescriptor]];

ascending:参数更改为NO以从高到低排序。将key:参数更改为@"name"以按名称排序。

修改

要创建模型对象数组,只需创建对象并用数据填充它。

Product *product = [Product alloc] init];
product.name = @"Widget";
product.price = @(3.65);
product.discount = @(.1);

这样一款Widget,售价3.65美元,折扣10%

然后将其添加到您的可变数组中。

NSMutableArray *productArray = [NSMutableArray array]; //create array only once 
for(thing in inboundInformation) {
    Product *product = //make and populate new product 
    [productArray addObject:product];
}

通常,您可以循环入站信息,例如JSON,并为每个Product创建一个对象,并将该对象添加到您的数组中进行演示。

答案 1 :(得分:2)

查看NSSortDescriptor

一些示例代码:

NSSortDescriptor *ageDescriptor = [[NSSortDescriptor alloc] initWithKey:@"age" ascending:YES];
NSSortDescriptor *hireDateDescriptor = [[NSSortDescriptor alloc] initWithKey:@"hireDate" ascending:YES];
NSArray *sortDescriptors = @[ageDescriptor, hireDateDescriptor];
NSArray *sortedArray = [employeesArray sortedArrayUsingDescriptors:sortDescriptors];

答案 2 :(得分:2)

"Price" key using to sorting the array. only we will change key. that key based sorting the array and return the values
for(int i=0;i<[price count];i++)
{
 NSMutableDictionary *cell=[[NSMutableDictionary alloc]init];
 [cell setObject:[name objectAtindex:i] forKey:@"Name"];
 [cell setObject:[percentage objectAtindex:i] forKey:@"Percentage"];
 [cell setObject:[price objectAtindex:i] forKey:@"Price"];
 [resultArray addObject:cell];
}
 NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"Price" ascending:YES];
 NSArray *sortedArray=[resultArray sortedArrayUsingDescriptors:@[sort]];

 for(int i=0;i<[sortedArray count];i++)
 {

 [price addObject:[[sortedArray objectAtIndex:i] objectForKey:@"Price"]];
 [percentage  addObject:[[sortedArray objectAtIndex:i]objectForKey:@"Percentage"]];
 [name addObject:[[sortedArray objectAtIndex:i]objectForKey:@"Name"]];

 }

 [tableview reload];

答案 3 :(得分:0)

NSMutableArray *arrayName=[[NSMutableArray alloc] initWithObjects:@"Ravi",@"Arpit",@"Rikin",@"Prerit", nil];

 NSLog(@"%@",[arrayName sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:nil ascending:YES]]]);

OR

[anArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]