Ravi Pathak Ravi Pathak - 2 months ago 13
iOS Question

How can i create array in ascending order when multiple Dictionary in Array

I have one Array with Multiple Dictionary object with keys

Date
and
Type

I want to arrange array in ascending order by
Date
key.

My Array Like this :

(
{
Date = "06 September 2016";
Type = Photo;
},
{
Date = "06 September 2016";
Type = Video;
},
{
Date = "07 September 2016";
Type = Photo;
},
{
Date = "07 September 2016";
Type = Video;
},
{
Date = "06 September 2016";
Type = Photo;
},
{
Date = "06 September 2016";
Type = Photo;
},
{
Date = "07 September 2016";
Type = Photo;
},
{
Date = "01 October 2016";
Type = Photo;
},
{
Date = "06 September 2016";
Type = Photo;
}
)


Please Help me. How can i Arrange In ascending order by Date.

Answer

Try This

NSSortDescriptor *sortDescriptor;
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Date"ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; 
NSArray *sortArray = [YourArray sortedArrayUsingDescriptors:sortDescriptors] ;
Comments