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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:29:52+00:00 2026-06-13T13:29:52+00:00

I have an NSArray containing several NSDictionary instances. Each NSDictionary has, among other fields,

  • 0

I have an NSArray containing several NSDictionary instances. Each NSDictionary has, among other fields, an object named rating and one numberOfVotes(both are int). How can I sort the array so it gets sorted by rating/numberOfVotes? More generically, can I sort it by doing an operation like mentioned above? Or would it be better to just add another object to each NSDictionary with the value of each operation and then sort by that?

Thanks

EDIT – I have added the following. Still not sorting properly. One question: Should this work for more than 2 objects in my array. (The number of objects will vary)

    [sortedArray sortedArrayUsingComparator:^NSComparisonResult(id dict1, id dict2)
{
    MyObj *obj1 = (MyObj *)dict1;
    MyObj *obj2 = (MyObj *)dict2;

    int rating1 = obj1.rating.intValue;
    int rating2 = obj2.rating.intValue;

    int number1 = obj1.number_of_votes.intValue;
    int number2 = obj2.number_of_votes.intValue;

    double key1 = ((double)rating1)/number1;
    double key2 = ((double)rating2)/number2;

    if (key1 < key2)
    {
        return NSOrderedDescending;
    }

    if (key2 < key1)
    {
        return NSOrderedAscending;
    }
    return NSOrderedSame;

}];
  • 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-13T13:29:53+00:00Added an answer on June 13, 2026 at 1:29 pm

    You can define a custom comparator to use a composite sorting key of the kind that you are looking for. If there is no good reason to have that sorting key in the dictionary, other than performing the sort, do not add an item to the dictionary.

    array = [array sortedArrayUsingComparator: ^(id obj1, id obj2) {
        int rating1 = [[obj1 objectForKey:@"rating"] intValue];
        int numberOfVotes1 = [[obj1 objectForKey:@"numberOfVotes"] intValue];
        int rating2 = [[obj2 objectForKey:@"rating"] intValue];
        int numberOfVotes2 = [[obj2 objectForKey:@"numberOfVotes"] intValue];
        double key1 = ((double)rating1)/numberOfVotes1;
        double key2 = ((double)rating2)/numberOfVotes2;
        if (key1 > key2) {
            return (NSComparisonResult)NSOrderedDescending;
        }
    
        if (key1 < key2) {
            return (NSComparisonResult)NSOrderedAscending;
        }
        return (NSComparisonResult)NSOrderedSame;
    }];
    

    Note: The sortedArrayUsingComparator: method does not sort the array in place; instead, it returns a sorted copy. If you would like an in-place sorting, use NSMutableArray.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two NSArray s each containing NSString s. I need to test whether
I have an NSArray storing NSDictionaries. Each dictionary has two keys; a name and
I am facing a very regular scenario. I have an NSArray which has object
i have a NSArray containing some NSDictionaries which themselves also include a NSDictionary .
I have an NSArray containing several NSDictionaries . I have an NSString containing an
I have an NSDictionary containing other NSDictionaries that I save to a plist. Its
I have a large NSArray containing NSNumbers like 3, 4, 20, 10, 1, 100,
I have a problem with sorting... I need to sort NSArray containing NSDictionaries .
I have an NSArray of NSDictionary objects which I would like to be able
I have this string (2.5+1)*5 I want to make a NSMutableArray containing each of

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.