Reputation: 33
hi i am new in IOS i have one array name with array1. Each index of an array there is one dictionary with 4 different fields like name,date,marks,standard. these all values are in NSString format. i want to sort this array1 by date. so can any one help me to do this please here below is some part of my code
NSMutableDictionary *tempDict = [[NSMutableDictionary alloc]init];
NSString *myname = name.text;
NSString *marks= marks.text;
NSString *date=date.text;
NSString *address=address.text;
NSString *rID=rID.text;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyMMdd"];
NSDate *date1 = [dateFormat dateFromString:date];
[tempDict setObject:myname forKey:@"name"];
[tempDict setObject:marks forKey:@"marks"];
[tempDict setObject:date1 forKey:@"date"];
[tempDict setObject:address forKey:@"address"];
[tempDict setObject:rID forKey:@"Rid"];
[array1 addObject:tempDict];
Upvotes: 0
Views: 219
Reputation: 458
I dont know about the size of your array but if it is not something very big, you can do this:
make a dictionary (we call it sortingDict
) with number of index (in array1) as key and your date of the corresponding array object as the value. You should iterate through your array1 to do this of course. (it would be better if you create NSDate objects of your string dates by the way).
get sortingDict
all values. ([sortingDict allValues]
) We call this dateArray
.
sort your date array using NSSortDescriptor
or any other algorithm you may want to use. We call it sortedDateArray
.
now in a for loop iterating through sortedDateArray
you get the keys of your sortingDict
in order (You can use [sortingDict allKeysForObject:]
and put it in an array sortedIndices
now you have an array with indices of array1
in your desired sorted order.
reordering your initial array1
wouldn't be a problem I believe.
P.S: This is a very inefficient way that just got out of the top of my head, hope it helps.
Upvotes: 0
Reputation: 86651
I think -sortUsingComparator: is the cleanest option.
[array1 sortUsingComparator: (id obj1, id obj2) {
return [[obj1 objectFoKey: @"date"] compare: [obj2 objectForKey: @"date"]];
}];
Upvotes: 0
Reputation: 39988
NSSortDescriptor *sortByDate = [NSSortDescriptor sortDescriptorWithKey:@"date"
ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortByDate];
NSArray *sortedArray = [array1 sortedArrayUsingDescriptors:sortDescriptors];
If you want to sort your array with the date then you have to create NSDate
object instead of NSString
NSDate *date = [dateFormatter dateFromString:date.text];
where dateFormatter is your NSDateFormatter
Upvotes: 2
Reputation: 354
you can do like this.. if you wanted to display in tableview than other arrays are useful.
if(array1 > 0)
{
NSMutableArray *UniqueDates = [array1 valueForKeyPath:@"@distinctUnionOfObjects.date"];
[recordDates addObjectsFromArray:UniqueDates];
NSArray *cleanedArray = [[NSSet setWithArray:recordDates] allObjects];
recordDates = [cleanedArray mutableCopy];
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:NO];
[recordDates sortUsingDescriptors:[NSArray arrayWithObjects:descriptor, nil]];
[mainarray addObjectsFromArray:array1];
}
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"date == %@", [recordDates objectAtIndex:YourIndex]];
// yourindex == indexpath.row if you are displaying in table view
NSArray *filterContacts= [array1 filteredArrayUsingPredicate:predicate];
mainarray = [filterContacts mutableCopy];
recordDates and mainarray is also mutablearray.
Upvotes: -1