Sorting one array in the same order as another array

前端 未结 2 735
渐次进展
渐次进展 2021-01-13 04:38

I have two arrays. Let\'s say:

array = \"Dave\", \"Mike\", \"Joe\", \"Jason\", \"Kevin\"

and

IQ = 110, 145, 75, 122, 130
         


        
相关标签:
2条回答
  • 2021-01-13 04:46

    Construct a 2d array with first row as names and second row as IQ Sort this array according to the IQ in O(nlogn)

    0 讨论(0)
  • 2021-01-13 04:47

    This would be a better way to make a dictionary for users. And then sorting based on their specific values like IQ, Name etc.

    NSArray *users = @[@"Dave",@"Mike",@"Joe",@"Jason",@"Kevin"];
    NSArray *iqs = @[@110,@145,@75,@122,@130];
    
    NSMutableArray *array = [NSMutableArray array];
    for (int idx = 0;idx<[users count];idx++) {
        NSDictionary *dict = @{@"Name": users[idx],@"IQ":iqs[idx]};
        [array addObject:dict];
    }
    
    NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"IQ" ascending:NO];
    [array sortUsingDescriptors:@[descriptor]];
    
    0 讨论(0)
提交回复
热议问题