2

Possible Duplicate:
How to sort NSMutableArray in objective c

i want to sort the NSMutablearray which contain the value 434,34,4,45

i am using this code for sorting:

[arrayfinale sortUsingSelector:@selector(compare:)];

but it return:

45,4,34,433

but i want

4,34,45,433

can anybody help me to solve this problem

Community
  • 1
  • 1
Jaspreet Singh
  • 1,180
  • 2
  • 12
  • 30

2 Answers2

5

from apple's documentation

NSArray *sortedArray = [array sortedArrayUsingComparator: ^(id obj1, id obj2) {

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

    if ([obj1 integerValue] < [obj2 integerValue]) {
        return (NSComparisonResult)NSOrderedAscending;
    }
    return (NSComparisonResult)NSOrderedSame;
}];
John Corbett
  • 1,595
  • 10
  • 19
3

Use Following code

   NSArray *unsortedArray = [NSArray arrayWithObjects:@"60", @"4", @"60", @"50", @"12", @"10", @"1", nil];
   NSArray *sortedArray = [unsortedArray sortedArrayUsingComparator:^(id firstObject, id secondObject) {
   return [((NSString *)firstObject) compare:((NSString *)secondObject) options:NSNumericSearch]; }];
   NSLog(@"Array is ::%@",sortedArray);
Anand
  • 3,346
  • 9
  • 35
  • 54