iOSDevp
iOSDevp

Reputation: 195

How to sort multiple NSArray ascending and decending order in ios

I've got three arrays, priceArray, nameArray & discountArray. I display these array values in a tableView. Each Cell has Price,Name,Discount.

I want to sort the list by Price Low to High and vice versa, the items in the nameArray and discountArray need to be sorted corresponding to the price sort.

Likewise I want to sort by name from A-Z and vice versa and sort price and discount correspondingly.

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%

Help me to sort like this.

Upvotes: 2

Views: 774

Answers (4)

HariKrishnan.P
HariKrishnan.P

Reputation: 1204

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

Upvotes: 2

Warren Burton
Warren Burton

Reputation: 17382

You cannot do what you are asking with 3 independent arrays. You need to gather all your information into a model object.

@interface Product:NSObject

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

@end

@implementation Product  
//this is empty 
@end

You can then use NSSortDescriptor to sort the array of Product.

For example by price low to high.

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

Change the ascending: argument to NO to sort high to low. Change the key: argument to @"name" to sort by name.

EDIT

To create your model object array , simply create the object and fill it with data.

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

So a Widget at $3.65 with 10% discount

Then add it to your mutable array.

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

Typically you would loop round inbound information e.g JSON and create an object for each Product and add that object to your array for presentation.

Upvotes: 8

Shivbaba&#39;s son
Shivbaba&#39;s son

Reputation: 1369

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

Upvotes: 0

Jasper
Jasper

Reputation: 7107

Have a look at NSSortDescriptor

Some sample code:

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

Upvotes: 2

Related Questions