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 に答える