使用group by two值对NsArray进行排序

时间:2013-10-14 05:51:01

标签: iphone ios nsarray

我对排序NsArray感到困惑,因为我在iOS开发中是非常新的,我有一个自定义数组,我想对我的NsArray进行排序,其值如下callTypenumberId分组:

    {
        callID = 1;
        callType = 4;
        dateTime = "2013-10-11 14:42:10 +0000";
        durationOfCall = "00:00:27";
        numberCalled = 91xxxxxxxxx;
        numberId = 8;
    },
    {
        callID = 1;
        callType = 4;
        dateTime = "2013-10-11 14:24:19 +0000";
        durationOfCall = "00:00:15";
        numberCalled = 91xxxxxxxxx;
        numberId = 9;
    },
    {
        callID = 1;
        callType = 4;
        dateTime = "2013-10-11 13:59:18 +0000";
        durationOfCall = "00:00:11";
        numberCalled = 91xxxxxxxxx;
        numberId = 8;
    },
    {
        callID = 1;
        callType = 4;
        dateTime = "2013-10-11 13:57:55 +0000";
        durationOfCall = "00:00:09";
        numberCalled = 91xxxxxxxxx;
        numberId = 8;
    },
    {
        callID = 1;
        callType = 4;
        dateTime = "2013-10-11 13:56:02 +0000";
        durationOfCall = "00:00:12";
        numberCalled = 91xxxxxxxxx;
        numberId = 8;
    },
    {
        callID = 1;
        callType = 4;
        dateTime = "2013-10-11 13:55:41 +0000";
        durationOfCall = "00:00:00";
        numberCalled = 91xxxxxxxxx;
        numberId = 8;
    }

)

结果应该是我得到每组中的计数

   {
        count = 4 // number of 
        callID = 1;
        callType = 4;
        dateTime = "2013-10-11 14:42:10 +0000";
        durationOfCall = "00:00:27";
        numberCalled = 91xxxxxxxxx;
        numberId = 8;
   },
   {
        count = 1 // number of 
        callID = 1;
        callType = 4;
        dateTime = "2013-10-11 14:24:19 +0000";
        durationOfCall = "00:00:27";
        numberCalled = 91xxxxxxxxx;
        numberId = 9;
   }
}

这是我上面的自定义类

#import <Foundation/Foundation.h>

@interface RecentCallsModel : NSObject


@property (nonatomic, retain) NSNumber *callID; // Integer 32
@property (nonatomic, retain) NSNumber *callType; // Integer32
@property (nonatomic, retain) NSString *numberId; // Either phone number or the contact id
@property (nonatomic, retain) NSDate *dateTime; //Date and time
@property (nonatomic, retain) NSString *durationOfCall;
@property (nonatomic, retain) NSString *numberCalled;


@end

1 个答案:

答案 0 :(得分:2)

它会起作用......

NSArray *sortedArray;
sortedArray = [array sortedArrayUsingComparator:^NSComparisonResult(id a, id b) 
{
    NSNumber *firstCallType = [(RecentCallsModel*)a CallType];
    NSNumber *secondCallType = [(RecentCallsModel*)b CallType];
    NSNumber *firstNumberID = [(RecentCallsModel*)a NumberID];
    NSNumber *second NumberID = [(RecentCallsModel*)b NumberID];
    if([firstCallType compare:secondCallType]==NSOrderedSame)
               {
                if([firstNumberID compare:secondNumberID]==NSOrderedSame)
                         {
                        //count logic
                         }
               else
                   return [firstNumberID compare:secondNumberID];
               }
   else
              return [firstCallType compare:secondCallType];
}];