bhavya kothari bhavya kothari - 2 days ago 4
iOS Question

how to sort below NSMutableArray structure

i want below array(10 elements) to be sorted ascending order of
"/Users/bkothari/Library....Photo-0.png" to be 0th element

of an array and so on

I have tried to use

NSArray *sorted = [profileImageArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
if ([obj1 intValue] < [obj2 intValue]) return NSOrderedAscending;
else return NSOrderedDescending;
}];


but its not working.

profileImageArray (
"/Users/bkothari/Library/Application Support/iPhone Simulator/7.0/Applications/233D4F5D-CDB0-4D55-8037-5DD674558BB0/Documents/MyFolder/Photo-4.png",
1,
1,
1,
1,
1,
1,
1,
"/Users/bkothari/Library/Application Support/iPhone Simulator/7.0/Applications/233D4F5D-CDB0-4D55-8037-5DD674558BB0/Documents/MyFolder/Photo-1.png",
"/Users/bkothari/Library/Application Support/iPhone Simulator/7.0/Applications/233D4F5D-CDB0-4D55-8037-5DD674558BB0/Documents/MyFolder/Photo-0.png"
)


Final output required:

profileImageArray (
"/Users/bkothari/Library/Application Support/iPhone Simulator/7.0/Applications/233D4F5D-CDB0-4D55-8037-5DD674558BB0/Documents/MyFolder/Photo-0.png",
"/Users/bkothari/Library/Application Support/iPhone Simulator/7.0/Applications/233D4F5D-CDB0-4D55-8037-5DD674558BB0/Documents/MyFolder/Photo-1.png",
1,
1,
"/Users/bkothari/Library/Application Support/iPhone Simulator/7.0/Applications/233D4F5D-CDB0-4D55-8037-5DD674558BB0/Documents/MyFolder/Photo-4.png",
1,
1,
1,
1,
1
)

Answer

Try with sortedArrayUsingComparator using NSNumericSearch option

try this:

profileImageArray = [profileImageArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        return [(NSString *)obj1 compare:(NSString *)obj2 options:NSNumericSearch];
}];
Comments