Getting Top 10 Highest Numbers From Array?

后端 未结 5 1396
轮回少年
轮回少年 2021-01-24 23:00

I am having a bit of a issue. I have an NSMutableDictionary with 10 NSMutableArrays in it. Each array has somewhere between 0-10 numbers which could each be any integer, e.g. 12

5条回答
  •  情深已故
    2021-01-24 23:36

    You have to short your array in descending order using 'C' logic. Here i'm going to give an example according to your condition....

    // adding 20 elements in an array, suppose this is your original array (array1).
    NSMutableArray *array1 = [[NSMutableArray alloc]init];
    
    for(int i=0;i<20;i++)
    {
        NSString *str = [NSString stringWithFormat:@"%d",(i*4)];
        [array1 addObject:str];
    }
    
    //make a copy of your original array 
    NSMutableArray *array2 = [[NSMutableArray alloc]initWithArray:array1];
    
    
    // this is the array which will get your sorting list
    NSMutableArray *array3 = [[NSMutableArray alloc]init];
    
    //declare an integer for compare as a maximum number and it to 0 initially
    int max = 0;
    
    
    // this is the logic to sort an array
    for(int i=0;i<20;i++)
    {
        for(int j=0;j<[array2 count];j++)
        {
            int f = [[array2 objectAtIndex:j] intValue];
            if(max

提交回复
热议问题