Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8549107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:38:58+00:00 2026-06-11T13:38:58+00:00

I have several arrays that need to be sorted side by side. For example,

  • 0

I have several arrays that need to be sorted side by side.

For example, the first array has names: @[@"Joe", @"Anna", @"Michael", @"Kim"], and
and the other array holds addresses: @[@"Hollywood bld", @"Some street 3", @"That other street", @"country road"], where the arrays’ indexes go together. “Joe” lives at “Hollywood bld” and so on.

I would like to sort the names array alphabetically, and then have the address array sorted alongside so they still go together, with “Hollywood bld” having same index as “Joe”. I know how to sort one array alphabetical with

NSSortDescriptor *sort=[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:NO];
[myArray sortUsingDescriptors:[NSArray arrayWithObject:sort]];

But is there any easy way of getting the second array sorted using the appropriate order?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T13:38:59+00:00Added an answer on June 11, 2026 at 1:38 pm
    1. Create a permutation array, initially set to p[i]=i
    2. Sort the permutation according to the name key of the first array
    3. Use the permutation to re-order both arrays

    Example: let’s say the first array is {"quick", "brown", "fox"}. The permutation starts as {0, 1, 2}, and becomes {1, 2, 0} after the sort. Now you can go through the permutation array, and re-order the original array and the second array as needed.

    NSArray *first = [NSArray arrayWithObjects: @"quick", @"brown", @"fox", @"jumps", nil];
    NSArray *second = [NSArray arrayWithObjects: @"jack", @"loves", @"my", @"sphinx", nil];
    NSMutableArray *p = [NSMutableArray arrayWithCapacity:first.count];
    for (NSUInteger i = 0 ; i != first.count ; i++) {
        [p addObject:[NSNumber numberWithInteger:i]];
    }
    [p sortWithOptions:0 usingComparator:^NSComparisonResult(id obj1, id obj2) {
        // Modify this to use [first objectAtIndex:[obj1 intValue]].name property
        NSString *lhs = [first objectAtIndex:[obj1 intValue]];
        // Same goes for the next line: use the name
        NSString *rhs = [first objectAtIndex:[obj2 intValue]];
        return [lhs compare:rhs];
    }];
    NSMutableArray *sortedFirst = [NSMutableArray arrayWithCapacity:first.count];
    NSMutableArray *sortedSecond = [NSMutableArray arrayWithCapacity:first.count];
    [p enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        NSUInteger pos = [obj intValue];
        [sortedFirst addObject:[first objectAtIndex:pos]];
        [sortedSecond addObject:[second objectAtIndex:pos]];
    }];
    NSLog(@"%@", sortedFirst);
    NSLog(@"%@", sortedSecond);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several arrays that are built similar to this: exampleArray=new array( A01 -
I have a 2D array that I have split up into several 1D arrays
I have a several arrays that I need to concat into a new one.
I am using jQuery 1.7.1 (but that's probably irrelevant). I have several JavaScript arrays
I have a mutable array that is retained and storing several objects. At some
I have a struct which has several arrays within it. The arrays have type
I have several array to deal with. I need to extract the most duplicate
I am creating several byte arrays that need to be joined together to create
I have several situations where I need to pass multi-dimensional PHP arrays into Javascript/jQuery.
I have several (27) images represented in 2D arrays that I am viewing with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.