0

我有如下的 NSDictionary 数组

array(
        {
        Birthdate = "03/20";
        Names = "Abhinavswati Chaudhary";
        remain = 0;
    },
        {
        Birthdate = "09/22/1986";
        Names = "Aisa Soni";
        remain = 186;
    },
        {
        Birthdate = "03/02/1990";
        Names = "Anuj Parekh";
        remain = 347;
    },
        {
        Birthdate = "05/22/1988";
        Names = "Ajay Desai";
        remain = 63;
    }
)

我怎样才能按照升序对该数组进行排序以保持例如它应该像

 array(
       {
       birthdate=...
       name =abhivan
       remanin =0;
   },
      {
       brithdate = "05/22/1988";
       name =ajay
       remain =63
   },
      {
       and so on....

请建议一些代码提前谢谢你

当我们使用 NSsortDiscripor 时,仍然需要是 NSNumber,这可能是我的剩余数字实际上是字符串的原因吗?

4

1 回答 1

2

这很简单,使用sortedArrayUsingComparatorNSArray 的方法。
在您的情况下,它应该如下所示:

yourArray = [yourArray sortedArrayUsingComparator:^NSComparisonResult(NSDictionary *obj1, NSDictionary *obj2) {
    // get values
    NSNumber *remain1 = obj1[@"remain"];
    NSNumber *remain2 = obj2[@"remain"];

    return [remain1 compare:remain2];
}];
于 2013-03-20T10:56:01.160 回答