user3652356
user3652356

Reputation: 33

Sorting NSMutableArray with date in ios7

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

Answers (4)

Mepla
Mepla

Reputation: 458

I dont know about the size of your array but if it is not something very big, you can do this:

  1. 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).

  2. get sortingDict all values. ([sortingDict allValues]) We call this dateArray.

  3. sort your date array using NSSortDescriptor or any other algorithm you may want to use. We call it sortedDateArray.

  4. 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

  5. now you have an array with indices of array1 in your desired sorted order.

  6. 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

JeremyP
JeremyP

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

Inder Kumar Rathore
Inder Kumar Rathore

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

Nisha
Nisha

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

Related Questions