0

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
)
6
  • 1
    What about those 1 elements in the array? They also seem to be ordered by a pattern. Commented Jan 2, 2014 at 13:33
  • have you try with NSNumericSearch option? Commented Jan 2, 2014 at 13:35
  • No they are dummy text. only paths must be sorted and dummy elements must be rearranged accordingly Commented Jan 2, 2014 at 13:37
  • I guess you want to sort data according to Photo-0.png, Photo-1.png, Photo-2.png ? Commented Jan 2, 2014 at 13:40
  • @bhavyakothari: Have you check my answer? Commented Jan 2, 2014 at 13:59

1 Answer 1

4

Try with sortedArrayUsingComparator using NSNumericSearch option

try this:

profileImageArray = [profileImageArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        return [(NSString *)obj1 compare:(NSString *)obj2 options:NSNumericSearch];
}];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.