按两个值对NSMutableArray进行排序

时间:2015-03-30 08:37:31

标签: ios objective-c sorting nsmutablearray nssortdescriptor

之前我在以下方式对NSMutableArray进行排序。

NSSortDescriptor *sortDescriptor1;
sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"Carton_IsDelivered"
                                             ascending:NO];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor1];
customerDetailsArray = [[customerDetailsArray sortedArrayUsingDescriptors:sortDescriptors] mutableCopy];

这是根据Carton_IsDelivered值进行排序,其值为true&错误的价值。

这很好。

现在根据新要求,我想按Carton_IsDelivered& Carton_AreaName

我想要的是

{p} Carton_IsDelivered=true分组Carton_AreaName Carton_IsDelivered=false

+ Carton_AreaName

我在下面使用过。

NSSortDescriptor *sortDescriptor1;
NSSortDescriptor *sortDescriptor2;
sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"Carton_IsDelivered"
                                             ascending:NO];
sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"Carton_AreaName"
                                              ascending:NO];
NSArray *sortDescriptors = [NSArray arrayWithObject:@[sortDescriptor1, sortDescriptor2]];
customerDetailsArray = [[customerDetailsArray sortedArrayUsingDescriptors:sortDescriptors] mutableCopy];

然而,应用程序崩溃说如下。

2015-03-30 11:35:45.402 Delivery[74822:9478073] -[__NSArrayI ascending]: unrecognized selector sent to instance 0x7ca5a660
2015-03-30 11:35:45.433 Delivery[74822:9478073] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI ascending]: unrecognized selector sent to instance 0x7ca5a660'
*** First throw call stack:
(
    0   CoreFoundation                      0x01f6f466 __exceptionPreprocess + 182
    1   libobjc.A.dylib                     0x01bf8a97 objc_exception_throw + 44
    2   CoreFoundation                      0x01f772c5 -[NSObject(NSObject) doesNotRecognizeSelector:] + 277
    3   CoreFoundation                      0x01ebfbc7 ___forwarding___ + 1047
    4   CoreFoundation                      0x01ebf78e _CF_forwarding_prep_0 + 14
    5   Foundation                          0x017d611e _NSInitializeCacheLine + 60
    6   Foundation                          0x017d5ee0 _sortedObjectsUsingDescriptors + 293
    7   Foundation                          0x017d5d51 -[NSArray(NSKeyValueSorting) sortedArrayUsingDescriptors:] + 713
    8   Delivery                            0x000d4388 -[DeliveryViewController bringDeliveredToTop] + 392
    9   Delivery                            0x000d0b4b -[DeliveryViewController parserDidEndDocument:] + 875
    10  Foundation                          0x018b1c34 _endDocument + 89
    11  libxml2.2.dylib                     0x057b6150 xmlParseChunk + 1366
    12  Foundation                          0x018afbf4 -[NSXMLParser finishIncrementalParse] + 58
    13  Foundation                          0x018afd95 -[NSXMLParser parseFromStream] + 385
    14  Foundation                          0x018afea8 -[NSXMLParser parse] + 33
    15  Delivery                            0x000cdfdc -[DeliveryViewController connectionDidFinishLoading:] + 1836
    16  CFNetwork                           0x05a1df39 ___ZL32_NSURLConnectionDidFinishLoadingP16_CFURLConnectionPKv_block_invoke + 40
    17  CFNetwork                           0x05a35af9 __65-[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:]_block_invoke + 83
    18  CFNetwork                           0x05a1cfe9 -[NSURLConnectionInternalConnection invokeForDelegate:] + 145
    19  CFNetwork                           0x05a35a93 -[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:] + 189
    20  CFNetwork                           0x05a35c46 -[NSURLConnectionInternal _withActiveConnectionAndDelegate:] + 58
    21  CFNetwork                           0x05a1c5e0 _ZL32_NSURLConnectionDidFinishLoadingP16_CFURLConnectionPKv + 43
    22  CFNetwork                           0x058e5fc5 ___ZN27URLConnectionClient_Classic26_delegate_didFinishLoadingEU13block_pointerFvvE_block_invoke + 115
    23  CFNetwork                           0x059c59ad ___ZN27URLConnectionClient_Classic18_withDelegateAsyncEPKcU13block_pointerFvP16_CFURLConnectionPK33CFURLConnectionClientCurrent_VMaxE_block_invoke_2 + 290
    24  CFNetwork                           0x058d064c _ZN19RunloopBlockContext13_invoke_blockEPKvPv + 70
    25  CoreFoundation                      0x01e63db9 CFArrayApplyFunction + 57
    26  CFNetwork                           0x058d050d _ZN19RunloopBlockContext7performEv + 149
    27  CFNetwork                           0x05a26fd6 _ZThn16_N19RunloopBlockContext24multiplexerClientPerformEv + 20
    28  CFNetwork                           0x058d0350 _ZN17MultiplexerSource7performEv + 292
    29  CFNetwork                           0x058d016c _ZN17MultiplexerSource8_performEPv + 76
    30  CoreFoundation                      0x01e9298f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    31  CoreFoundation                      0x01e8849d __CFRunLoopDoSources0 + 253
    32  CoreFoundation                      0x01e879f8 __CFRunLoopRun + 952
    33  CoreFoundation                      0x01e8737b CFRunLoopRunSpecific + 443
    34  CoreFoundation                      0x01e871ab CFRunLoopRunInMode + 123
    35  GraphicsServices                    0x0424d2c1 GSEventRunModal + 192
    36  GraphicsServices                    0x0424d0fe GSEventRun + 104
    37  UIKit                               0x005be9b6 UIApplicationMain + 1526
    38  Delivery                            0x000b4882 main + 130
    39  libdyld.dylib                       0x04772ac9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

知道我做错了吗?

2 个答案:

答案 0 :(得分:2)

据我所知你使用只需要一个参数的arrayWithObject,并且你传递数组所以你有一个包含另一个数组的数组,你需要只有一个包含两个对象的数组,试试这个:< / p>

NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor1, sortDescriptor2, nil];

答案 1 :(得分:0)

将arrayWithObject:更改为arrayWithObjects:

NSArray *sortDescriptors = [NSArray arrayWithObjects:@[sortDescriptor1, sortDescriptor2]];