0

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];
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Well sorting by date when it is a string is going to work, since it will be ordered as a string not date. You might want to store the date as a date. – rckoenes May 19 '14 at 11:19
  • but i can also convert NSString to NSDate here. but don't know how to sort – user3652356 May 19 '14 at 11:24
  • Look at this for help: http://stackoverflow.com/a/1134126/2043580 – ZeMoon May 19 '14 at 11:27
  • To sort an NSArray you consult the documentation for NSArray and NSMutableArray. You have about a half-dozen options. Please learn how to use the documentation. – Hot Licks May 19 '14 at 12:01

4 Answers4

2
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

David Snabel-Caunt
  • 57,804
  • 13
  • 114
  • 132
Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184
  • but its working with string format i mean jun 2014 is come after august2014.in short it is working on string format... – user3652356 May 19 '14 at 11:34
  • @user3652356 [tempDict setObject:date forKey:@"date"]; in this date object should be of type nsdate not nsstring, then it will work. – Pawan Rai May 19 '14 at 11:38
  • 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"]; but still not working – user3652356 May 19 '14 at 12:09
  • Please check your date object is being created – Inder Kumar Rathore May 19 '14 at 12:13
  • @user3652356 you can go through this question for reference http://stackoverflow.com/q/805547/468724 – Inder Kumar Rathore May 19 '14 at 12:16
  • @user3652356 here is a simple working sample, do check this code http://pastebin.com/hn06HhVv – Inder Kumar Rathore May 19 '14 at 12:33
0

I think -sortUsingComparator: is the cleanest option.

[array1 sortUsingComparator: (id obj1, id obj2) {
    return [[obj1 objectFoKey: @"date"] compare: [obj2 objectForKey: @"date"]];
}];
JeremyP
  • 84,577
  • 15
  • 123
  • 161
0

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.

Mepla
  • 438
  • 4
  • 16
-1

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.

Nisha
  • 354
  • 2
  • 19
  • @BryanChen ya its little complicated but it is useful for those who wanted to display section in tableview of unique dates which is sorted. i don't know putting extra helpful code get minus rank otherwise i will not put that code [recordDates count]; // can be number of section – Nisha May 19 '14 at 12:10