2015-06-11 207 views
2

我有三個數組,priceArray,nameArray & discountArray。我在tableView中顯示這些數組值。每個單元都有價格,名稱,折扣。如何在ios中對多個NSArray升序和降序排序進行排序

我想按價格從低到高排列列表,反之亦然,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% 

請幫我這樣排序。

+2

應該有一個封裝價格,名稱和折扣價值的自定義類型的數組。你的生活會變得更簡單。 – Droppy

+1

使用sortDescriptor學習 –

回答

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]; 
+0

如果你只有數組然後在鍵寫nil的地方,它會對數組進行排序。 –

+0

@ArpitParekh輸入nil時,它如何對數組進行排序呢?按字母順序? – Jasper

+0

如果你把零放在密鑰的位置,它將按升序或降序排序。像這樣 –

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:)] 
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]; 
} 

通常你會循環輪入站信息e.g JSON和創建的每個Product一個對象,這個對象添加到您的陣列呈現。

+0

你能幫我解釋一下如何將這些值存儲在模型對象中的單個數組中 – iOSDevp

+0

這是正確的方法。 – squarefrog

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];