简体   繁体   English

排序nsarray

[英]sorting the nsarray

I have an NSArray as given below: 我有一个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;
    }
)

I want to sort that array based on the the field "Timeregistered" but I was unable to sort properly and my code is as follows: 我想根据“ 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];

the array is sorting but it is sorting with 2.59pm,2.57pm,4.14pm,4.12pm etc, 阵列正在排序,但正在以2.59pm,2.57pm,4.14pm,4.12pm等进行排序,

But I want the array to be sorted in 4.14,4.12,2.59,2.57 etc, 但我想将数组按4.14、4.12、2.59、2.57等排序,

I am converting string into time but still facing the same problem. 我正在将字符串转换为时间,但仍然面临相同的问题。

Could someone please help me? 有人可以帮我吗?

It looks like you want to reverse the sort if you have already assorted it or something. 如果您已经对它进行了分类或类似的操作,则似乎要反转排序。 If you want it to be sorted in the reverse order, return the opposite comparison result that you currently have 如果希望以相反的顺序进行排序,请返回当前具有的相反比较结果

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];
         }
    }];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM