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 9234109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:42:46+00:00 2026-06-18T06:42:46+00:00

I got two nsmutablearrays one is an array of distances and another is an

  • 0

I got two nsmutablearrays one is an array of distances and another is an array containing the images linked to these distances. I need to sort the distances array in ascending order and then sort the images array based on that. I tried to make a dictionary of these 2 arrays with distance array as the key, but when the distance value is same, the same image is returned in the sorted image array. Can anyone help me with this issue.
The code is as follows:

 // Put the two arrays into a dictionary as keys and values
    NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:_locationImages forKeys:_distances];
    // Sort the first array
    NSArray *sortedFirstArray = [[dictionary allKeys] sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        if ([obj1 floatValue] > [obj2 floatValue])
            return NSOrderedDescending;
        else if ([obj1 floatValue] < [obj2 floatValue])
            return NSOrderedAscending;
        return NSOrderedSame;
    }];
    // Sort the second array based on the sorted first array
    NSArray *sortedSecondArray = [dictionary objectsForKeys:sortedFirstArray notFoundMarker:[NSNull null]];           
  • 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-18T06:42:47+00:00Added an answer on June 18, 2026 at 6:42 am

    You do not need the distance as the key. Just have an array of dictionaries. Somehow this also makes it easier to sort.

    NSMutableArray *dataArray = [NSMutableArray array];
    for (int i=0; i<distanceArray.count; i++) {
        [dataArray addObject:@{
           @"distance" : distanceArray[i],
           @"image"    : imageArray[i]
        }];
    }
    NSArray *sorted = [dataArray sortedArrayUsingDescriptors:@[
                       [NSSortDescriptor sortDescriptorWithKey:@"distance"
                                                     ascending:YES]]];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a simple object post that has two NSMutableArrays as properties. One is
I got two class, one reference another by using @Reference When inserting I will
Got two textbox's one represents the form's width, and the another height. When the
Iv got two DB tables. One containing types(Id, Name) and the other holds datapoints
I got two tables (one with 33k rows and another with 7k rows). I
I got two data storages in my Windows Azure account. One is for the
I got two apps, with these package names: com.blah.a com.blah.b They got the sharedUserId
I got two tables, one of which is a helper of the other. The
I'm trying to create a sectioned table. I've got two parsed NSMutableArrays feedsTopics and
Ive got two DIV elements one of which has absolute position (lower left corner

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.