Anand
Anand

Reputation: 1129

Sort NSMutableArray containing numbers in string forrmat

I am having a small Problem in sorting NSMUtableArray containing NSStrings that contains int. I am sorting like this

    scoreString=[NSString stringWithFormat:@"%lld",GameScore];
    [scorearr addObject:scoreString];
    NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self" ascending: NO];
    NSArray *scoreArray= [scorearr sortedArrayUsingDescriptors: [NSArray arrayWithObject: sortOrder]];
    scorearr =(NSMutableArray *)ary;

Upvotes: 0

Views: 1319

Answers (2)

xxcv
xxcv

Reputation: 331

You're using mutable array so it's better to write

NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self" ascending: NO];
[scorearr sortUsingDescriptors:[NSArray arrayWithObject: sortOrder]];

That way you're not creating new array when sorting.

Upvotes: 1

Anand
Anand

Reputation: 1129

Ok i am Done with this,

    NSNumber *inrank=[NSNumber numberWithInt:GameScore];
    scoreString=[NSString stringWithFormat:@"%lld",GameScore];
    [scorearr addObject:inrank];
    NSLog(@"%@",scorearr.description);
    //sorting array
    sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
    NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self" ascending: NO];
    NSArray *scoreArray= [scorearr sortedArrayUsingDescriptors: [NSArray arrayWithObject: sortOrder]];
    scorearr =(NSMutableArray *)scoreArray;
    NSLog(@"%@",scoreArray.description);

Upvotes: 0

Related Questions