如何使用两个键值对nsmutablearray进行排序

时间:2015-08-28 04:54:10

标签: ios objective-c sorting nsmutablearray nssortdescriptor

我的NSMutableArray值如下

我想使用" category_name"按字母顺序对此数组进行排序和total_assets> 0.00

建议我一些排序代码

{
    CID = 1;
    "category_name" = Art;
    "rbg_color" = "1,187,212";
    "total_assets" = "12500.00";
},
{
    CID = 2;
    "category_name" = Automobile;
    "rbg_color" = "244,66,54";
    "total_assets" = "102600.00";
},
{
    CID = 3;
    "category_name" = Banks;
    "rbg_color" = "1,149,135";
    "total_assets" = "0.00";
},
{
    CID = 4;
    "category_name" = Cash;
    "rbg_color" = "75,175,79";
    "total_assets" = "25000.00";
},
{
    CID = 5;
    "category_name" = Clothing;
    "rbg_color" = "103,57,182";
    "total_assets" = "0.00";
},
{
    CID = 7;
    "category_name" = Electronics;
    "rbg_color" = "32,149,242";
    "total_assets" = "0.00";
},

{
    CID = 10;
    "category_name" = Watches;
    "rbg_color" = "139,194,74";
    "total_assets" = "0.00";
},
{
    CID = 11;
    "category_name" = Wine;
    "rbg_color" = "62,80,180";
    "total_assets" = "30500.00";
}

4 个答案:

答案 0 :(得分:1)

您的朋友NSPredicatNSSortDescriptor您可以在提供的链接中找到两者的Apple文档,以下是一种简单的方法:

-(void)sortMeyhod
{
    NSSortDescriptor *sortDescriptor;
   //here you initialize the descriptor with key category_name , and it is case insensitive i.e capital or small
   sortDescriptor=[[NSSortDescriptor alloc] initWithKey:@"category_name"
                                           ascending:YES
                                           selector:@selector(caseInsensitiveCompare:)];
   //create your descriptors array .
   sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
   //sort your array . 
   yourArray  = [yourArray sortedArrayUsingDescriptors:sortDescriptors];
    //filter your array with a predicate . 
   NSPredicate * predicate = [NSPredicate predicateWithFormat:@"%K > 0.00" , @"total_assets"] ;
   yourArray =[yourArray filteredArrayUsingPredicate:predicate] ; 
}

我没有对代码进行全面测试,但从理论上讲它应该可行,并且请参阅文档。

答案 1 :(得分:1)

NSSortDescriptor *sortDescriptor  = [[NSSortDescriptor alloc] initWithKey:@"category_name" ascending:YES];
NSArray *sortedArray = [myArray sortedArrayUsingDescriptors:@[sortDescriptor]];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K > 0.00" , @"total_assets"] ;
sortedArray = [sortedArray filteredArrayUsingPredicate:predicate];

NSLog(@"%@",sortedArray);

答案 2 :(得分:0)

使用此,

NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"category_name" ascending:YES];
tempArray=[yourArray sortedArrayUsingDescriptors:@[sort]];

NSSortDescriptor *sort1 = [NSSortDescriptor sortDescriptorWithKey:@"total_assets" ascending:YES];
sortedArray=[tempArray sortedArrayUsingDescriptors:@[sort1]];

答案 3 :(得分:0)

最后解决了:

NSSortDescriptor *sortDescriptor  = [[NSSortDescriptor alloc] initWithKey:@"category_name" ascending:YES];
NSArray *sortedCatg = [myArray sortedArrayUsingDescriptors:@[sortDescriptor]];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K > %@" , @"total_assets",@"0.00"] ;
sortedCatg = [sortedCatg filteredArrayUsingPredicate:predicate];

NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"%K = %@" , @"total_assets",@"0.00"] ;
NSArray *sortedAssets = [myArray filteredArrayUsingPredicate:predicate1];

myArray = [[NSMutableArray alloc]init];
[myArray addObjectsFromArray:sortedCatg];
[myArray addObjectsFromArray:sortedAssets];