使用2个排序描述符对数组进行排序

时间:2016-03-06 15:20:35

标签: ios objective-c arrays sorting nsdate

我有一组NSDictionary个对象,每个对象都有一个EventDateSecurityLevel。我想先按日期排序,然后按安全级别排序。我怎么能这样做?

Obj1: SecurityLevel: 5, Date: 20/05/2015 22:03
Obj2: SecurityLevel: 5, Date: 05/03/2015 05:28
Obj3: SecurityLevel: 4, Date: 14/04/2015 11:01
Obj4: SecurityLevel: 4, Date: 07/08/2015 09:31
Obj6: SecurityLevel: 3, Date: 24/04/2015 21:06
Obj5: SecurityLevel: 3, Date: 29/01/2016 22:38
Obj7: SecurityLevel: 2, Date: 02/06/2015 20:49
Obj8: SecurityLevel: 2, Date: 13/07/2015 17:46
Obj9: SecurityLevel: 1, Date: 19/08/2015 07:57

他们应该这样排序:

Obj1: SecurityLevel: 5, Date: 20/05/2015 22:03
Obj2: SecurityLevel: 5, Date: 05/03/2015 05:28
Obj4: SecurityLevel: 4, Date: 07/08/2015 09:31
Obj3: SecurityLevel: 4, Date: 14/04/2015 11:01
Obj5: SecurityLevel: 3, Date: 29/01/2016 22:38
Obj6: SecurityLevel: 3, Date: 24/04/2015 21:06
Obj8: SecurityLevel: 2, Date: 13/07/2015 17:46
Obj7: SecurityLevel: 2, Date: 02/06/2015 20:49
Obj9: SecurityLevel: 1, Date: 19/08/2015 07:57

我做了类似的事情,但是没有按预期工作:

- (NSArray*)sortEventsByDateAndSecurity:(NSArray*)toSort
{
    NSSortDescriptor *securityDescriptor = [[NSSortDescriptor alloc] initWithKey:@"SeverityLevel" ascending:NO];
    NSArray *sortDescriptors = @[securityDescriptor];
    NSArray *sortedSecurity = [toSort sortedArrayUsingDescriptors:sortDescriptors];

    NSArray *reverseOrderUsingComparator = [sortedSecurity sortedArrayUsingComparator:^(id obj1, id obj2) {

        NSDictionary *dictObject1 = (NSDictionary*)obj1;
        NSDictionary *dictObject2 = (NSDictionary*)obj2;

        NSDate *obj1Date = [Utils stringToDate:[dictObject1 valueForKey:@"EventDate"] format:@"dd/MM/yyyy HH:mm"];
        NSDate *obj2Date = [Utils stringToDate:[dictObject2 valueForKey:@"EventDate"] format:@"dd/MM/yyyy HH:mm"];

        return [obj1Date compare:obj2Date];
    }];

    return reverseOrderUsingComparator;
}

1 个答案:

答案 0 :(得分:1)

您只需使用2个描述符数组而不是1:

调用$ javac abcd.java $ java abcd Rows: 3 and Columns: 4 # # # # # # ---------------------------- Rows: 5 and Columns: 6 # # # # # # # # # # # # # # # ---------------------------- $
if ( $(window).width() < 481) {

var mycity=new google.maps.LatLng(51.xxx, 0.xxx);
function initialize()
{
var mapProp = {
 center:mycity,
 zoom:10,
 mapTypeId:google.maps.MapTypeId.ROADMAP
 };
var map = new google.maps.Map(document.getElementById("map-canvas"),mapProp);
var myCity = new google.maps.Circle({
 center:mycity,
 radius:32186,
 strokeColor:"#0000FF",
 strokeOpacity:0.8,
 strokeWeight:2,
 fillColor:"#00FF00",       <!--lime green mobile-->
 fillOpacity:0.2
 });
myCity.setMap(map);
}
google.maps.event.addDomListener(window, 'load', initialize);

} 
else {

var mycity=new google.maps.LatLng(51.xxx, 0.xxx);
function initialize()
{
var mapProp = {
 center:mycity,
 zoom:11,
 mapTypeId:google.maps.MapTypeId.ROADMAP
 };
var map = new google.maps.Map(document.getElementById("map-canvas"),mapProp);
var myCity = new google.maps.Circle({
 center:Pembury,
 radius:32186,
 strokeColor:"#0000FF",
 strokeOpacity:0.8,
 strokeWeight:2,
 fillColor:"#FF00FF",     <!--magenta desktop-->
 fillOpacity:0.2
 });
myCity.setMap(map);
}
google.maps.event.addDomListener(window, 'load', initialize);
}

在这种情况下,第二个排序描述符将仅应用于在第一个排序描述符方面相等的对象。对于您的情况,这意味着具有相同sortedArrayUsingDescriptors:的对象将按- (NSArray*)sortEventsByDateAndSecurity:(NSArray*)toSort { NSSortDescriptor *securityDescriptor = [[NSSortDescriptor alloc] initWithKey:@"SeverityLevel" ascending:NO]; NSSortDescriptor *eventDateDescriptor = [[NSSortDescriptor alloc] initWithKey:@"EventDate" ascending:NO comparator:^NSComparisonResult(NSString *obj1, NSString *obj2) { NSDate *obj1Date = [Utils stringToDate:obj1 format:@"dd/MM/yyyy HH:mm"]; NSDate *obj2Date = [Utils stringToDate:obj2 format:@"dd/MM/yyyy HH:mm"]; return [obj1Date compare:obj2Date]; }]; NSArray *sortDescriptors = @[securityDescriptor, eventDateDescriptor]; return [toSort sortedArrayUsingDescriptors:sortDescriptors]; } 排序。

相关问题