How to sort NSArray strings in date format @ "Aug 2013" in ascending order.?

I have an array of NSString objects in the format ... @"Aug 2013" @"July 2013" and so on...

I want to sort this array in ascending order .. Is getting a date first for an individual value a good idea ..?

+4
source share
7 answers

Another solution in one block

 NSArray* array = [NSArray arrayWithObjects:@"Janvier 2012",@"Mars 2013", @"Juin 2013", @"Octobre 2012", nil]; NSArray* result = [array sortedArrayUsingComparator:^(id a, id b) { NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"MMMM YYYY"]; NSDate* datea = [dateFormat dateFromString:a]; NSDate* dateb = [dateFormat dateFromString:b]; return [datea compare:dateb]; }]; 
+5
source

I see that you prefer "strict" (not typos) typed variables. They often end up giving you such an error. What you really have to do is store dates as dates. They compare well with each other, and you can do date calculations with them. The same is true for numbers.

Then, if you want to present them as “August 2013” ​​or “July 2013” ​​in the user interface: use the date format to format the dates in the corresponding lines for display only.


In this case, the two main classes that you should study (read the documentation, etc.) are the following: NSDate and NSDateFormatter . NSCalendar and NSDateComponents should also be useful.

+3
source

I address this issue using the category to switch between NSDate and NSString. I would solve this problem by converting to dates using categories, sorting and subsequent conversion.

My category is as follows (you will need to add similar methods to control the format of the month / year that your rows have)

  #import "NSDate+Utils.h" @implementation NSDate (NSDate_Utils) + (NSDate *)dateFromI18NString:(NSString *)dateString { if (dateString == nil) return nil; NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss ZZZ"; NSDate *d = [dateFormatter dateFromString:dateString]; return d; } - (NSString *)dateToI18NString { NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss ZZZ"; return [dateFormatter stringFromDate:self]; } 
+3
source

I would use NSArray from NSDate objects and format its contents where necessary.

+2
source

You can sort the array by running the code below.

 -(void)somemethod{ NSArray *sortedArray = [[NSArray alloc]initWithArray:YOURARRAY]; sortedArray = [sortedArray sortedArrayUsingFunction:dateSort context:nil]; } 

Call the function below as described above to sort your array. (Update the format according to your array date format)

 NSComparisonResult dateSort(NSString *s1, NSString *s2, void *context) { NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"MMMM yyyy"]; NSDate *d1 = [formatter dateFromString:s1]; NSDate *d2 = [formatter dateFromString:s2]; return [d1 compare:d2]; // ascending order return [d2 compare:d1]; // descending order } 

Hope this can help you.

+1
source

try it

 - (void)viewDidLoad { [super viewDidLoad]; NSArray *unsortedArray =[[NSArray alloc]initWithObjects:@"July 2013",@"Aug 2013",@"Jun 2012", nil]; NSMutableArray *arrSorted = [NSMutableArray arrayWithArray:unsortedArray]; for(int i=0;i<[arrSorted count];i++) { for(int j=i+1;j<[arrSorted count];j++) { NSDateFormatter *df=[[NSDateFormatter alloc] init]; df.dateFormat=@ "MMM yyyy"; NSDate *date1=[df dateFromString:[arrSorted objectAtIndex:i]]; NSDate *date2=[df dateFromString:[arrSorted objectAtIndex:j]]; if([date1 compare:date2]== NSOrderedAscending) { [arrSorted exchangeObjectAtIndex:i withObjectAtIndex:j]; } } } // ORDER_DESCEND NSLog(@"%@",arrSorted); arrSorted = [NSMutableArray arrayWithArray:[[arrSorted reverseObjectEnumerator] allObjects]]; // ORDER_ASCEND NSLog(@"%@",arrSorted); } 
+1
source
 - (NSArray *)sortedDateArrayWithExistedArray:(NSMutableArray *) setExistedArray acending:(BOOL)ascendingBoolvalue { NSArray *sortedArray = [setExistedArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) { NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"dd MMM, yyyy"]; NSDate *d1 = [formatter dateFromString:obj1[@"displayDate"]]; NSDate *d2 = [formatter dateFromString:obj2[@"displayDate"]]; // id = [d1 compare:d2]; if (ascendingBoolvalue) { return [d1 compare:d2]; } else { return [d2 compare:d1]; } }]; return sortedArray; } 

set setExistedArray == yourArray set the ascending parameter as yes or no based on your requirement yes for the ascending order no to decreasing.

how to access this function

self.sortedArray = [[self sortedDateArrayWithExistedArray: self.yourArray Acquisition: NO] mutableCopy];

if yourArray is a mutable call forwarding method, otherwise there is no need

0
source

Source: https://habr.com/ru/post/1500145/


All Articles