简体   繁体   中英

sorting the nsarray

I have an NSArray as given below:

(
        {
        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:

 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,

But I want the array to be sorted in 4.14,4.12,2.59,2.57 etc,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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