2014-01-28 30 views
0

我有一個帶日期鍵和名稱鍵的可變字典日期鍵有三種方式的日期one-expired date,two-coming soon date and nil (0000-00-00 YYYY-MM-DD)。 現在我想按順序排列這個NSDictionary數組。 即將到來的日期(升序)。 過期日期。 其他可能爲零我用比較器和排序數組以2格式一個數組沒有空值,一個是有值並追加這些數組但沒有顯示數組值的問題以升序顯示過期先顯示...sort NSMutableDictionary包含日期字符串Json的對象

NSMutableDictionary *JsonDict=[NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&err]; 
// NSLog(@"jsondata :%@",JsonDict); 

NSArray *arrayData=[[NSArray alloc]initWithArray:[JsonDict objectForKey:@"Product"]]; 

NSMutableArray* filtered =[[NSMutableArray alloc] init]; 
NSMutableArray*unfiltered =[[NSMutableArray alloc] init]; 
    NSMutableArray*unfiltered1 =[[NSMutableArray alloc] init]; 

for (int i=0; i<[arrayData count]; i++) 
{ 

    NSMutableDictionary *reviewsDict123=[arrayData objectAtIndex:i]; 

    if ([[reviewsDict123 objectForKey:@"date"]isEqualToString:@"0000-00-00"]) 
    { 

     [filtered addObject:reviewsDict123]; 

    } 

    else 
    { 

     [unfiltered addObject:reviewsDict123]; 


    } 

} 

未經過濾的數組在2013年和2014年具有當前值的混合值,所以2013年的價值已過期,2014年的價值可用,所以我需要對2014年的價值升序排序並過期將在另一個數組中。

NSSortDescriptor *sortDescriptors = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:NO]; 
sortedArray = [unfiltered sortedArrayUsingDescriptors:sortDescriptors]; 

排序包含第一到期,現在我要重新排序此,如何

+3

的NSDictionary是可可的容器,不可排序。 – Desdenova

+0

我在nsmutable字典中有json對象,只是在nsarray中傳輸,請檢查編輯問題 – user2638242

+0

@Desdenova,雖然這是真的,但也有方法通過將其值複製到數組或類似的東西來排序字典的值。 – Hless

回答

0

糾正我已經實現了你的情況下,用以下方式

創建給我輸入(在你的情況Json的一個測試方法響應字典)

- (void)testDataForApplicationis{ 

NSMutableDictionary *testDict=[[NSMutableDictionary alloc]init]; 
NSMutableArray *testArray=[[NSMutableArray alloc]init]; 
[testDict setObject:@"1" forKey:@"id"]; 
[testDict setObject:@"Abcd" forKey:@"name_en"]; 
[testDict setObject:@"adfasdfasdfadsf" forKey:@"description_en"]; 
[testDict setObject:@"2014-01-19 00:00:00" forKey:@"start_date"]; 
[testDict setObject:@"2014-01-19 00:00:00" forKey:@"end_date"]; 
[testArray addObject:testDict]; 

testDict=[[NSMutableDictionary alloc]init]; 
[testDict setObject:@"2" forKey:@"id"]; 
[testDict setObject:@"Abcd" forKey:@"name_en"]; 
[testDict setObject:@"adfasdfasdfadsf" forKey:@"description_en"]; 
[testDict setObject:@"2014-01-20 00:00:00" forKey:@"start_date"]; 
[testDict setObject:@"2014-01-25 00:00:00" forKey:@"end_date"]; 
[testArray addObject:testDict]; 


testDict=[[NSMutableDictionary alloc]init]; 
[testDict setObject:@"2" forKey:@"id"]; 
[testDict setObject:@"Abcd" forKey:@"name_en"]; 
[testDict setObject:@"adfasdfasdfadsf" forKey:@"description_en"]; 
[testDict setObject:@"2013-12-20 00:00:00" forKey:@"start_date"]; 
[testDict setObject:@"2013-12-01 00:00:00" forKey:@"end_date"]; 
[testArray addObject:testDict]; 

testDict=[[NSMutableDictionary alloc]init]; 
[testDict setObject:@"4" forKey:@"id"]; 
[testDict setObject:@"Abcd" forKey:@"name_en"]; 
[testDict setObject:@"adfasdfasdfadsf" forKey:@"description_en"]; 
[testDict setObject:@"2013-08-01 00:00:00" forKey:@"start_date"]; 
[testDict setObject:@"2013-08-25 00:00:00" forKey:@"end_date"]; 
[testArray addObject:testDict]; 
NSMutableArray *promotionModlArray=[PromotionModel getPromotionModelObject:testArray]; 
NSMutableArray *sortArray=[self sortPromotionListWithEndDate:promotionModlArray]; 
} 

模型類在地圖字典中的對象和綁定日期和othere細節與一個對象

這裏類名是PromotionModel在這裏.h文件中

@interface PromotionModel : NSObject 
@property (nonatomic,strong) NSString *promotionid; 
@property (nonatomic,strong) NSString *promotionName; 
@property (nonatomic,strong) NSString *promotionDesc; 
@property (nonatomic,strong) NSString *promotionStartDate; 
@property (nonatomic,strong) NSString *promotionEndDate; 

+ (NSMutableArray*)getPromotionModelObject:(NSMutableArray*)prmotionDataArray; 

在PromotionModel .m文件地圖字典,通過形式試驗方法

+ (NSMutableArray*)getPromotionModelObject:(NSMutableArray*)prmotionDataArray{ 

NSMutableArray *prmotionDeatilArray=[[NSMutableArray alloc]init]; 
PromotionModel *promotionModel=nil; 
for (NSMutableDictionary *promotinDetailDict in prmotionDataArray) { 
    promotionModel=[[PromotionModel alloc]init]; 
    promotionModel.promotionid=[promotinDetailDict objectForKey:@"id"]; 

    promotionModel.promotionName=[promotinDetailDict objectForKey:@"name_en"]; 
    promotionModel.promotionDesc=[promotinDetailDict objectForKey:@"description_en"]; 


    promotionModel.promotionStartDate=[promotinDetailDict objectForKey:@"start_date"]; 
    promotionModel.promotionEndDate=[promotinDetailDict objectForKey:@"end_date"]; 
    [prmotionDeatilArray addObject:promotionModel]; 
} 
    return prmotionDeatilArray; 
} 

這裏模型類完成

現在在這裏用相關方法分選邏輯

-(NSMutableArray *)sortPromotionListWithEndDate:(NSMutableArray *)unsortedArray{ 
NSMutableArray *tempArray=[NSMutableArray array]; 
for(int i=0;i<[unsortedArray count];i++) 
{ 
    PromotionModel *model =[unsortedArray objectAtIndex:i]; 

    NSString *dateStr=[self getDateinstaderdForm:model.promotionEndDate]; 
    NSDate *date=[self getDateFromString:dateStr]; 
    NSMutableDictionary *dict=[NSMutableDictionary dictionary]; 
    [dict setObject:model forKey:@"entity"]; 
    [dict setObject:date forKey:@"date"]; 
    [tempArray addObject:dict]; 
} 

NSInteger counter=[tempArray count]; 
NSDate *compareDate; 
NSInteger index; 
for(int i=0;i<counter;i++) 
{ 
    index=i; 
    compareDate=[[tempArray objectAtIndex:i] objectForKey:@"date"]; 
    NSDate *compareDateSecond; 
    for(int j=i+1;j<counter;j++) 
    { 
     compareDateSecond=[[tempArray objectAtIndex:j] objectForKey:@"date"]; 
     NSComparisonResult result = [compareDateSecond compare:compareDate]; 
     if(result == NSOrderedDescending) 
     { 
      compareDate=compareDateSecond; 
      index=j; 
     } 
    } 
    if(i!=index) 
     [tempArray exchangeObjectAtIndex:i withObjectAtIndex:index]; 
} 


[unsortedArray removeAllObjects]; 
for(int i=0;i<[tempArray count];i++) 
{ 
    [unsortedArray addObject:[[tempArray objectAtIndex:i] objectForKey:@"entity"]]; 
} 
for (PromotionModel *model in unsortedArray) { 
    NSLog(@" --------- New Object -------"); 
    NSLog(@"Promotion Name %@",model.promotionid); 
    NSLog(@"Promotion Sort Details %@",model.promotionEndDate); 
    NSLog(@"Promotion Name %@",model.promotionName); 

} 

return unsortedArray; 

} 

支持在邏輯中使用的方法For date formate.This用於將所有日期設置爲匹配的相同格式。

- (NSString*)getDateinstaderdForm:(NSString*)currDate{ 
NSDateFormatter *df=[[NSDateFormatter alloc]init]; 
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 
NSDate *originalDate=[df dateFromString:currDate]; 
[df setDateFormat:@"yyyy-MM-dd"]; 
NSString *dateStr=[df stringFromDate:originalDate]; 
return dateStr; 
} 



-(NSDate *) getDateFromString : (NSString *)stringDate 
{ 

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; 
//[dateFormatter setTimeZone:[NSTimeZone localTimeZone]]; 
//[dateFormatter setDateFormat:@"MM/dd/yyyy"]; 
[dateFormatter setDateFormat:@"yyyy-MM-dd"]; 

NSDate *date = [dateFormatter dateFromString: stringDate]; 
return date; 
} 

我出把

--------- New Object ------- 
2014-01-29 19:39:54.628 StackOverFlowSolutions[4756:a0b] Promotion Name 2 
2014-01-29 19:39:54.639 StackOverFlowSolutions[4756:a0b] Promotion Sort Details 2014-01-25 00:00:00 
2014-01-29 19:39:54.643 StackOverFlowSolutions[4756:a0b] Promotion Name Abcd 
2014-01-29 19:39:54.643 StackOverFlowSolutions[4756:a0b] --------- New Object ------- 
2014-01-29 19:39:54.644 StackOverFlowSolutions[4756:a0b] Promotion Name 1 
2014-01-29 19:39:54.645 StackOverFlowSolutions[4756:a0b] Promotion Sort Details  2014-01-19 00:00:00 
2014-01-29 19:39:54.645 StackOverFlowSolutions[4756:a0b] Promotion Name Abcd 
2014-01-29 19:39:54.646 StackOverFlowSolutions[4756:a0b] --------- New Object ------- 
2014-01-29 19:39:54.646 StackOverFlowSolutions[4756:a0b] Promotion Name 2 
2014-01-29 19:39:54.658 StackOverFlowSolutions[4756:a0b] Promotion Sort Details 2013-12-01 00:00:00 
2014-01-29 19:39:54.661 StackOverFlowSolutions[4756:a0b] Promotion Name Abcd 
2014-01-29 19:39:54.662 StackOverFlowSolutions[4756:a0b] --------- New Object ------- 
2014-01-29 19:39:54.663 StackOverFlowSolutions[4756:a0b] Promotion Name 4 
2014-01-29 19:39:54.663 StackOverFlowSolutions[4756:a0b] Promotion Sort Details 2013-08-25 00:00:00 
2014-01-29 19:39:54.670 StackOverFlowSolutions[4756:a0b] Promotion Name Abcd 

試試這個,這是工作希望這給你得到所需的輸出

+0

我有8個鍵不僅名稱和日期其8,我必須在NSUrlConnection方法中對此進行排序。 – user2638242

+0

@ user2638242您需要將您的字典映射到模型對象的編輯代碼。 – user1548843

+0

請告訴我MAPMotionModel,MAObject的含義?兩者都是你的控制器iam調用方法使用NSMutableArray * dataArray = [pramotion getPromotionModelObject:unfiltered];這裏的推廣是我的課 – user2638242

相關問題