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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:17:53+00:00 2026-06-14T01:17:53+00:00

I have an NSMutableArray with 70 objects, they are NSMutableDictionary , inside each dictionary

  • 0

I have an NSMutableArray with 70 objects, they are NSMutableDictionary, inside each dictionary I have 2 values: “distance” and “indexPath”. I want to sort the NSMutableArray from the small distance to the biggest. The distance is in KM and the array looks like this after using the code that I pasted here:

NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"distance" ascending:YES];
[branchDistance sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]];

This is the result of this method:

(lldb) po [branchDistance objectAtIndex:0]
(id) $1 = 0x0d6ac240 {
    "<NSIndexPath 0xd6cf180> 1 indexes [0]" = indexPath;
    "8.078547" = distance;
}
(lldb) po [branchDistance objectAtIndex:1]
(id) $2 = 0x0d6a64a0 {
    "<NSIndexPath 0xd6cf3b0> 1 indexes [1]" = indexPath;
    "45.044069" = distance;
}
(lldb) po [bran(lldb) po [branchDistance objectAtIndex:2]
(id) $4 = 0x0d6cf550 {
    "<NSIndexPath 0xd69b3f0> 1 indexes [2]" = indexPath;
    "9.992081" = distance;
}
(lldb) po [branchDistance objectAtIndex:3]
(id) $5 = 0x0d6cf750 {
    "283.356727" = distance;
    "<NSIndexPath 0xd6cf480> 1 indexes [3]" = indexPath;
}

I want to sort the NSMutableArray from the small number to the big number, using this “distance” key value inside the NSMutableDictionary. I tried few pieces of code from the web and from stackoverflow, but couldn’t find something that worked for me.

Please help me!

Thanks!

  • 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-14T01:17:54+00:00Added an answer on June 14, 2026 at 1:17 am

    I assume the distance values are stored in the dictionaries using an NSNumber object:

    NSArray *sortedArray = [branchDistance sortedArrayUsingComparator:^(id obj1, id obj2) {
        NSNumber *distance1 = [(NSDictionary *)obj1 objectForKey:@"distance"];
        NSNumber *distance2 = [(NSDictionary *)obj2 objectForKey:@"distance"];
        return [distance1 compare:distance2];
    }];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a NSMUtableArray and a NSMutableDictionary . My code gets objects from an
I want to add objects from NSArray *small whenever they don´t exist in NSArray
I have an NSMutableArray which has five objects in it. I want to insert
I have a NSMutableArray pages with several objects (instances of Page ). Each Page
I have a NSMutableArray called putNumberUsed. It contains the following objects @blah1,@blah2,@blah3,@blah4. I want
Say I have NSMutableArray *array1 with 10 objects. I want to create an *array2
I have NSMutableArray with different objects in it of different classes. Now I want
I have an NSMutableArray of objects. Each object has a property called Name. I
I have a NSMutableArray with objects of type NSMutableDictionary , the NSMutableDictionary contains 2
So I want to remove a whole series of objects from my DB. They

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.