0

我想尝试按NSMutableDictionary's value which key is pricein people which is排序NSMutableArray

像这样...

people = [
  ["price" => 133, "name" => "banana"],
  ["price" => 333,"name" => "apple"],
  ["price" => 3, "name" => "orange"],
]

我想要这样:

copiedPeople = [
  ["price" => 3, "name" => "orange],
  ["price" => 133,"name" => "banana"],
  ["price" => 333, "name" => "apple"],
]

你有什么主意吗?

4

1 回答 1

1

sortUsingComparator:让您传入一个说明如何对项目进行排序的块。所以你可以这样做:

[people sortUsingComparator: ^NSComparisonResult(id obj1, id obj2) {
    return [[obj1 objectForKey: @"price"] compare: [obj2 objectForKey: @"price"]];
}];

编辑:刚刚看到你的编辑......你可以sortedArrayUsingComparator:在 NSArray 上使用来创建一个新的排序数组。

于 2012-05-15T13:07:55.483 回答