2013-03-06 126 views
0

我有一個NSArray下面給出:排序的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 = "[email protected]"; 
     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 = "[email protected]"; 
     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點59分,下午2時57分,下午四點14分選,下午4時12分等等,

但我想數組在4.14,4.12,2.59,2.57等進行排序,

我將字符串轉換爲時間,但仍面臨同樣的問題。

有人能幫我嗎?

+0

檢查,如果您的日期得到正確的值。 – karim 2013-03-06 15:17:10

回答

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