0

i have a NSArray and arrays are numbers like this;

NSArray *ratio = [NSArray arrayWithObjects:@"99",@"88",@"77", @"66", @"55", @"44",@"33", @"22",@"11",@"0",@"-11",@"-22",@"-33",@"-44",@"-55",@"-66",@"-77",@"-88",@"-99",@"-100",nil];

i want to take biggest object 99 and smallest object -100 hiw can i do this ?

i will use if condition biggest object like this ;

if ([[ratio objectAtIndex:i] intValue] == 100 ) {

            rd = 0;
            gr = 0.5;
            bl =0;
}

//i want to do this

if ([[ratio objectAtIndex:i] intValue] == biggestobject or smallestobject ) {
      ///.....
}

Help pls myfriends:( - Now solve problem but there is a another problem at follows.

Second one problem :(

NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self" ascending: NO];
NSLog(@"%@",[ratio sortedArrayUsingDescriptors: [NSArray arrayWithObject: sortOrder]]);

i sorted array but cannot choose biggest and smallest object from array. How can i do this?

NSLog screen is following like this; 99, 99, 99, 91, 91, 88, 88, 88, 88, 77, 77, 66, 66, 66,

how can i take objectforkey:0 at sorted array

DShah
  • 9,768
  • 11
  • 71
  • 127
coexploit
  • 29
  • 5

4 Answers4

1

One way would be to just sort the array and take the first and last index.

Sort an NSArray in Descending Order

Community
  • 1
  • 1
tjg184
  • 4,508
  • 1
  • 27
  • 54
1

will this solve your issue:

NSInteger maxValue = 0, minValue = 0;
BOOL firstIteration = YES;

for(id item in ratio) {
    NSInteger intValue = [item integerValue];
    if( firstIteration ) {
        maxValue = intValue;
        minValue = intValue;
        firstIteration = NO;
        continue;
    }

    if( intValue < minValue ) minValue = intValue;

    if( intValue > maxValue ) maxValue = intValue;
 }
Denis
  • 6,313
  • 1
  • 28
  • 27
0

Write this method to sort two strings as you have defined, note the NSNumericSearch . See Documentation here.

NSComparisonResult sortNums(NSString *s1, NSString *s2, void *context) {    
        return [s2 compare:s1 options:NSNumericSearch];
    }

then sort using:

NSArray *sorted = [unsorted sortedArrayUsingFunction:sortNums context:nil];
zakishaheen
  • 5,551
  • 1
  • 22
  • 29
0

to get the values, instead the following code:

NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self" ascending: NO];
NSLog(@"%@",[ratio sortedArrayUsingDescriptors: [NSArray arrayWithObject: sortOrder]]);

you need to use:

NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self" ascending: NO]; 
NSArray *sortedArray = [ratio sortedArrayUsingDescriptors: [NSArray arrayWithObject: sortOrder]];

NSLog(@"max item: %@, min item: %@", [soretedArray objectAtIndex:0], [sortedArray lastObject]);
Denis
  • 6,313
  • 1
  • 28
  • 27