Yuvaraj.M
Yuvaraj.M

Reputation: 9801

How to sort an array with numbers in iphone?

I need your help. I have stored selected indexpath values in an NSMutableArray from UITableView. I have stored these values in NSString type and i want to retrieve like NSString to int datatype. But, I want to sort this array by (0,1,2,3,4,5,6,7,8,9,10,11,12,13....)this way. Currently am getting by this format(0,1,10,11,12,2,3,4,5,6,7,8,9..) I dont know how to sort this array? Please any one help me friends? Thanks for spending your valuable time with me and read my poor english.

Thanks is advance.

Upvotes: 0

Views: 263

Answers (1)

viggio24
viggio24

Reputation: 12366

Let' say your mutable array is called "myArray". Then you cannot simply sort its content using string comparator but you must convert strings to decimals and use the values to sort. So the sorting call will be, using blocks:


[myArray sortUsingComparator: ^(id obj1, id obj2) {

    if ([obj1 integerValue] > [obj2 integerValue]) {
        return (NSComparisonResult)NSOrderedDescending;
    }

    if ([obj1 integerValue] < [obj2 integerValue]) {
        return (NSComparisonResult)NSOrderedAscending;
    }
    return (NSComparisonResult)NSOrderedSame;
}];

Upvotes: 1

Related Questions