-3

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

    {
        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
4

1 回答 1

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];
}];
于 2013-10-14T08:55:53.133 回答