Wednesday, 28 December 2011

Sort NSMutableArray according to Key



<array>
   <dict>
       name =  "zain"
   <dict/>
   <dict>
       name =  "fahad"
   <dict/>
   <dict>
   name =  "Aslam"
   <dict/>
    <dict>
   name =  "kashif"
   <dict/>
<array/>





-(NSMutableArray *)sortArrayAccordingTokey:(NSMutableArray *)array
{
    NSSortDescriptor *sortDescriptor;
    sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES] autorelease];
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
    NSArray *sortedArray = [array sortedArrayUsingDescriptors:sortDescriptors];
    [array removeAllObjects];
    [array addObjectsFromArray:sortedArray];
    return array;
}

No comments:

Post a Comment