0

我有一个 NSArray,如下所示:

(
        {
        AppStatus = Appointment;
        AptEndTime = "03/06/2013  9:30 PM";
        AptStartTime = "03/06/2013  9:00 PM";
        BirthDate = "03/06/1968";
        Email = "";
        FirstName = Mobile;
        IsSpouse = 0;
        LastName = Development;
        MiddleInitial = Ap;
        PatientID = "";
        Phone = "";
        ScacntronCSVId = 937515;
        SlotID = 2;
        Timeregistered = "03/06/2013  4:12 PM";
        UserName = "Mob_03062013122612";
    },
        {
        AppStatus = Appointment;
        AptEndTime = "03/06/2013 12:30 PM";
        AptStartTime = "03/06/2013 12:00 PM";
        BirthDate = "03/06/1980";
        Email = "";
        FirstName = Test;
        IsSpouse = 0;
        LastName = Iphone;
        MiddleInitial = "";
        PatientID = "";
        Phone = "";
        ScacntronCSVId = 937514;
        SlotID = 1;
        Timeregistered = "03/06/2013  4:24 PM";
        UserName = "Tes_03062013122113";
    },
        {
        AppStatus = Walkin;
        AptEndTime = "";
        AptStartTime = "";
        BirthDate = "03/06/1990";
        Email = "";
        FirstName = Fn;
        IsSpouse = 0;
        LastName = Ln;
        MiddleInitial = "";
        PatientID = "";
        Phone = "";
        ScacntronCSVId = 937519;
        SlotID = 5;
        Timeregistered = "03/06/2013  6:40 PM";
        UserName = "FnL_03062013183612";
    },
        {
        AppStatus = Appointment;
        AptEndTime = "05/26/2013 12:30 PM";
        AptStartTime = "05/26/2013 12:00 PM";
        BirthDate = "03/06/1978";
        Email = "angad.d@technosoftcorp.com";
        FirstName = Kathy;
        IsSpouse = 0;
        LastName = Ybarra;
        MiddleInitial = "";
        PatientID = "";
        Phone = 3452345235;
        ScacntronCSVId = 937516;
        SlotID = 3;
        Timeregistered = "03/06/2013  2:57 PM";
        UserName = Rajeesth0099;
    },
        {
        AppStatus = Appointment;
        AptEndTime = "02/28/2013 10:30 AM";
        AptStartTime = "02/28/2013 10:00 AM";
        BirthDate = "03/06/1944";
        Email = "angad.d@technosoftcorp.com";
        FirstName = herohonda;
        IsSpouse = 0;
        LastName = Woodpicker;
        MiddleInitial = "";
        PatientID = "";
        Phone = 5234523523;
        ScacntronCSVId = 937517;
        SlotID = 4;
        Timeregistered = "03/06/2013  2:59 PM";
        UserName = Vinitkumar007;
    }
)

我想根据字段“Timeregistered”对该数组进行排序,但我无法正确排序,我的代码如下:

 NSArray *sortedAllDataArray = [temp sortedArrayUsingComparator:^NSComparisonResult(NSDictionary *dict1, NSDictionary *dict2) {

        NSDateFormatter *df = [[NSDateFormatter alloc] init];

        df.dateFormat = @"MM/dd/yyyy HH:mm a";
        df.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];

        if (appoineSorted == YES) {

            //For Ascending Order
            return [[df dateFromString:[dict1 objectForKey:@"AptStartTime"]] compare:[df dateFromString:[dict2 objectForKey:@"AptStartTime"]]];

        }else{

            //For Desending Order
            return -1 *[[df dateFromString:[dict1 objectForKey:@"Timeregistered"]] compare:[df dateFromString:[dict2 objectForKey:@"Timeregistered"]]];
        }
    }];


    temp = [sortedAllDataArray mutableCopy];

数组正在排序,但它正在排序 2.59pm、2.57pm、4.14pm、4.12pm 等,

但我希望数组按 4.14、4.12、2.59、2.57 等排序,

我正在将字符串转换为时间,但仍然面临同样的问题。

有人可以帮我吗?

4

1 回答 1

0

如果您已经对其进行了分类或其他内容,那么您似乎想要反转排序。如果您希望它以相反的顺序排序,请返回您当前拥有的相反比较结果

NSArray *sortedAllDataArray = [temp sortedArrayUsingComparator:^NSComparisonResult(NSDictionary *dict1, NSDictionary *dict2) {

        NSDateFormatter *df = [[NSDateFormatter alloc] init];

        [df setDateFormat:@"MM/dd/yyyy HH:mm a"];
        [df setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

        NSDate *date1 = [df dateFromString:[dict1 objectForKey:@"Timeregistered"]];
        NSDate *date2 = [df dateFromString:[dict2 objectForKey:@"Timeregistered"]];

        if (appoineSorted == YES) {
            appoineSorted = NO;
            return [date2 compare:date1];
         } else {
            appoineSorted = YES;
            return [date1 compare:date2];
         }
    }];
于 2013-03-06T15:05:02.777 回答