按两个值对 NSMutableArray 进行排序
Sort NSMutableArray by two values
早些时候我用下面的方式对 NSMutableArray 进行排序。
NSSortDescriptor *sortDescriptor1;
sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"Carton_IsDelivered"
ascending:NO];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor1];
customerDetailsArray = [[customerDetailsArray sortedArrayUsingDescriptors:sortDescriptors] mutableCopy];
这是基于 Carton_IsDelivered
值的排序,其中包含真值和假值。
这工作正常。
现在根据新要求,我想按 Carton_IsDelivered
& Carton_AreaName
排序。
我要的是
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
知道我做错了什么吗?
据我所知,您使用的 arrayWithObject
只接受一个参数,并且您传递了数组,因此您的数组包含另一个数组,您只需要一个包含两个对象的数组,试试这个:
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor1, sortDescriptor2, nil];
将 arrayWithObject: 更改为 arrayWithObjects:
NSArray *sortDescriptors = [NSArray arrayWithObjects:@[sortDescriptor1, sortDescriptor2]];
早些时候我用下面的方式对 NSMutableArray 进行排序。
NSSortDescriptor *sortDescriptor1;
sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"Carton_IsDelivered"
ascending:NO];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor1];
customerDetailsArray = [[customerDetailsArray sortedArrayUsingDescriptors:sortDescriptors] mutableCopy];
这是基于 Carton_IsDelivered
值的排序,其中包含真值和假值。
这工作正常。
现在根据新要求,我想按 Carton_IsDelivered
& Carton_AreaName
排序。
我要的是
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
知道我做错了什么吗?
据我所知,您使用的 arrayWithObject
只接受一个参数,并且您传递了数组,因此您的数组包含另一个数组,您只需要一个包含两个对象的数组,试试这个:
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor1, sortDescriptor2, nil];
将 arrayWithObject: 更改为 arrayWithObjects:
NSArray *sortDescriptors = [NSArray arrayWithObjects:@[sortDescriptor1, sortDescriptor2]];