2012-03-01 84 views
58

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

这是我的数组:

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

通常用于对数组排序我会用

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

但是如何排序使用字典的brand关键?

回答

105

我想这会做到这一点:

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

我把代码Sort Descriptor Programming Topics。此外,Key-Value Coding发挥作用,因为sortedArrayUsingDescriptors:将向myArray中的每个元素发送valueForKey:,然后使用标准比较器对返回的值进行排序。

+0

如何在swift2中使用这个? – 2015-11-20 06:44:47

1

只是尝试一下最简单的方法...

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

你可以做到这一点。

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

我们得到的解决方案通过使用方法遵循

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

其中: -

jsonData - MutableArray持有解析的JSON数据。

fullname - 我们要分类的数据。

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

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

虽然sortedArrayUsingComparator是要走的路,但最多有四次调用“valueForKey”而不是两次调用“objectForKey”是不好的。 BTW。既然你期望字典,使用NSDictionary *作为类型,而不是id。 – gnasher729 2015-11-19 09:58:32

3

作为除了QED的代码,

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

这澄清变量的类和优化用快速枚举数组创建。 感谢

6

在switf:

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

使用下面的排序使用“品牌”从字典中关键代码..

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

使用下面的代码,如果你要排序根据两个键从字典中;就像,“品牌”从字典中键和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); 
2

我的代码使用NSSortDescriptor时被撞毁,从而结束了使用这在我的使用情况,在那里我期待的“等级”的伟大工程块成为一个NSNumber。如果对象不能转换为整数,它不会进行排序,但它也不会导致崩溃。

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