Vipin Saini
Vipin Saini

Reputation: 67

Sorting an NSArray that contains int with particular int

I have some data like this :

1, 5, 2, 9, 7, 6, 3, 8, 0, 4

but I actually want this:

5, 6, 7, 8, 9

only start to 5 .

and I use this:

NSArray *ary = @[ @"1", @"5", @"2", @"9", @"7", @"6", @"3", @"8", @"0", @"4" ];
NSArray *myArray = [ary sortedArrayUsingDescriptors:
                    @[[NSSortDescriptor sortDescriptorWithKey:@"doubleValue" ascending:YES]]];

Upvotes: 0

Views: 60

Answers (2)

Avi
Avi

Reputation: 7552

This solution treats the contents as numeric, as suggested by the question's sample code.

double startingValue = 5;

NSArray *ary = @[ @"1", @"5", @"2", @"9", @"7", @"6", @"3", @"8", @"0", @"4" ];

NSArray *myArray= [ary filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self.doubleValue >= %@", @(startingValue)]];

myArray = [myArray sortedArrayUsingDescriptors:
                @[[NSSortDescriptor sortDescriptorWithKey:@"doubleValue" ascending:YES]]];

Upvotes: 0

Yagnesh Dobariya
Yagnesh Dobariya

Reputation: 2251

NSString *numberExpr =@"^[5-9]";
NSArray *ary = @[ @"1", @"5", @"2", @"9", @"7", @"6", @"3", @"8", @"0", @"4" ];

NSArray *myArray= [ary filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self  MATCHES %@",numberExpr]];

Upvotes: 1

Related Questions