按字典中的键值对字典的NSArray进行sorting

我有一个由字典填充的数组,我需要按照字典的其中一个键的值按字母顺序对数组进行sorting。

这是我的数组:

tu dictus: ( { brand = Ryul; productTitle = Any; quantity = 1; subBrand = "Ryul INJ"; type = Product; }, { brand = Trol; productTitle = Different; quantity = 2; subBrand = ""; type = Brand; }, { brand = Dtor; productTitle = Any; quantity = 1; subBrand = ""; type = Product; }, { brand = Ryul; productTitle = Different; quantity = 2; subBrand = "Ryul CHES"; type = SubBrand; }, { brand = Anan; productTitle = Any; quantity = 1; subBrand = ""; type = Product; } ) 

正常情况下,我将使用sorting数组

 myArray = [uniqueProdsArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; 

但是如何使用字典的brand关键词进行sorting呢?

我认为这将做到这一点:

 brandDescriptor = [[NSSortDescriptor alloc] initWithKey:@"brand" ascending:YES]; sortDescriptors = [NSArray arrayWithObject:brandDescriptor]; sortedArray = [myArray sortedArrayUsingDescriptors:sortDescriptors]; 

我从sorting描述符编程主题中提取代码。 此外, Key-Value Coding也会发挥作用,在sortedArrayUsingDescriptors:将发送一个valueForKey:myArray每个元素,然后使用标准比较器对返回的值进行sorting。

我们通过使用方法得到了解决scheme

 [self.jsonData sortUsingDescriptors: [NSArray arrayWithObjects: [NSSortDescriptor sortDescriptorWithKey:"fullname" ascending:YES], [NSSortDescriptor sortDescriptorWithKey:"id" ascending:NO], nil]]; 

哪里:-

jsonData – MutableArray其中包含parsingJSON数据。

fullname – 我们要sorting的数据。

id – 内部字典附带的唯一数据。

在switf:

 var descriptor: NSSortDescriptor = NSSortDescriptor(key: "brand", ascending: true) var sortedResults: NSArray = results.sortedArrayUsingDescriptors([descriptor]) 

使用以下代码进行sorting,使用词典中的“品牌”键。

 NSSortDescriptor * brandDescriptor = [[NSSortDescriptor alloc] initWithKey:@"brand" ascending:YES]; NSArray * sortDescriptors = [NSArray arrayWithObject:brandDescriptor]; NSArray * sortedArray = [myArray sortedArrayUsingDescriptors:sortDescriptors]; NSLog(@"sortedArray %@",sortedArray); 

使用下面的代码,如果你按字典中的两个键进行sorting; 就像字典中的“品牌”键和productTitle键一样: –

 NSSortDescriptor * brandDescriptor = [[NSSortDescriptor alloc] initWithKey:@"brand" ascending:YES]; NSSortDescriptor * productTitleDescriptor = [[NSSortDescriptor alloc] initWithKey:@"productTitle" ascending:YES]; NSArray * sortDescriptors = [NSArray arrayWithObjects:brandDescriptor, productTitleDescriptor, nil]; NSArray * sortedArray = [feedData sortedArrayUsingDescriptors:sortDescriptors]; NSLog(@"sortedArray %@",sortedArray); 
  arrSorted = [arrBrand sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) { if ([[obj1 valueForKey:@"iUserId"] integerValue] > [[obj2 valueForKey:@"iUserId"] integerValue]) { return (NSComparisonResult)NSOrderedDescending; } if ([[obj1 valueForKey:@"iUserId"] integerValue] < [[obj2 valueForKey:@"iUserId"] integerValue]) { return (NSComparisonResult)NSOrderedAscending; } return (NSComparisonResult)NSOrderedSame; }]; 

作为QED代码的补充,

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

这澄清了variables的类,并通过快速枚举优化了数组的创build。 谢谢

我的代码在使用NSSortDescriptor时崩溃了,所以最后使用了一个在我的用例中很好用的块,我期待“rank”是一个NSNumber。 如果对象无法转换为整数,则不会进行sorting,但也不会导致崩溃。

 NSArray *sortedArray = [data sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) { long data1 = [[obj1 valueForKey:@"rank"] integerValue]; long data2 = [[obj2 valueForKey:@"rank"] integerValue]; if (data1 > data2) { return (NSComparisonResult)NSOrderedDescending; } if (data1 < data2) { return (NSComparisonResult)NSOrderedAscending; } return (NSComparisonResult)NSOrderedSame; }]; 
 NSSortDescriptor *brandDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"Position" ascending:YES selector:@selector(localizedStandardCompare:)] autorelease]; NSArray *sortDescriptors = [NSArray arrayWithObject:brandDescriptor]; NSArray *sortedArray = [arrTemp sortedArrayUsingDescriptors:sortDescriptors]; array_PreLagData=(NSMutableArray*)sortedArray; unsorted array Printing description of arrTemp: <__NSArrayM 0x10282100>( { Milker2 = "11:03:17 AM"; Position = 2; }, { Milker1 = "11:03:28 AM"; Position = 25; }, { Milker3 = "11:03:18 AM"; Position = 3; }, { Milker1 = "11:03:16 AM"; Position = 1; Strip = "11:32:32 AM"; }, { Milker1 = "11:03:21 AM"; Position = 10; } ) Sorted array <__NSArrayI 0x101363c0>( { Milker1 = "11:03:16 AM"; Position = 1; Strip = "11:32:32 AM"; }, { Milker2 = "11:03:17 AM"; Position = 2; }, { Milker3 = "11:03:18 AM"; Position = 3; }, { Milker1 = "11:03:21 AM"; Position = 10; }, { Milker1 = "11:03:28 AM"; Position = 25; } ) [enter link description here][1] [1]: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/SortDescriptors/Articles/Creating.html#//apple_ref/doc/uid/20001845-BAJEAIEE 

你可以这样做 。

 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"d LLLL yyyy"]; NSComparator compareDates = ^(id string1, id string2) { NSDate *date1 = [formatter dateFromString:string1]; NSDate *date2 = [formatter dateFromString:string2]; return [date1 compare:date2]; }; NSSortDescriptor * sortDesc1 = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:NO comparator:compareDates]; [array sortUsingDescriptors:[NSArray arrayWithObjects:sortDesc1, nil]]; 

试试看最简单的方法

 myArray = [[NSMutableArray alloc] init]; NSMutableArray *tempArray = [[NSMutableArray alloc] init]; [tempArray removeAllObjects]; [tempArray addObjectsFromArray: myArray]; NSString *key = @"brand"; NSSortDescriptor *brandDescriptor = [[NSSortDescriptor alloc] initWithKey:key ascending:YES]; NSArray *sortDescriptors = [NSArray arrayWithObjects:brandDescriptor,nil]; NSArray *sortedArray = [tempArray sortedArrayUsingDescriptors:sortDescriptors]; [brandDescriptor release]; [tempArray removeAllObjects]; tempArray = (NSMutableArray*)sortedArray; [myArray removeAllObjects]; [myArray addObjectsFromArray:tempArray];