5

I have two arrays. Let's say:

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

and

IQ = 110, 145, 75, 122, 130

I want to sort them by IQ. Say Highest to lowest. I can sort one array... and then I go back and check to see what position it was in and then rearrange the other array. It seems like there must be a better way to do this. Especially if the array gets larger.

Here is how I'm doing it right now.

d1, d2, d3, d4, d5 are my IQ variable. I use the sortBack array to rearrange another array in the same order.

NSMutableArray *myArray = [NSMutableArray arrayWithObjects:[NSString stringWithFormat:@"%d",d1], [NSString stringWithFormat:@"%d",d2],[NSString stringWithFormat:@"%d",d3], [NSString stringWithFormat:@"%d",d4],[NSString stringWithFormat:@"%d",d5], nil];

//sorting
[myArray sortUsingComparator:^NSComparisonResult(NSString *str1, NSString *str2) {
    return [str1 compare:str2 options:(NSNumericSearch)];
}];

for(int i=0;i<5;i++)
{
    if([[myArray objectAtIndex:i] integerValue]==d1)
    {
        sortBackArray[i]=1;
    }
    else if([[myArray objectAtIndex:i] integerValue]==d2)
    {
        sortBackArray[i]=2;
    }
    else if([[myArray objectAtIndex:i] integerValue]==d3)
    {
        sortBackArray[i]=3;
    }
    else if([[myArray objectAtIndex:i] integerValue]==d4)
    {
        sortBackArray[i]=4;
    }
    else if([[myArray objectAtIndex:i] integerValue]==d5)
    {
        sortBackArray[i]=5;
    }
}
3
  • 6
    Why are you maintaining two arrays for this. It's better to have an array of dictionaries with name and IQ values. You can then create a sortDescriptor with key IQ and sort the array. When you have a related info, always keep them together. Commented May 23, 2013 at 9:31
  • Yea, I thought that might be the way to go, but I tend to get confused with the dictionary stuff. I'll take a look at it again. Commented May 23, 2013 at 9:35
  • It would be very difficult if you are not making use of dictionaries or custom model objects to keep data structured. I have posted an example. Take a look. Commented May 23, 2013 at 9:42

2 Answers 2

19

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]];
Sign up to request clarification or add additional context in comments.

1 Comment

Actually i want this type of sort based on date....Thanks for saving my time ....it works like magic!!!!
3

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.