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

  • Home
  • SEARCH
  • 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 7520045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:59:57+00:00 2026-05-30T01:59:57+00:00

I have problem with sort array of arrays. I already build method to sort

  • 0

I have problem with sort array of arrays. I already build method to sort but it not work properly. I mean that final table should be sorted by last element, descending using the last element.

My method:

static NSInteger order (id a, id b, void* context) 
        {
        NSNumber* catA = [a lastObject];
        NSNumber* catB = [b lastObject];
        return [ catB compare: catA];
        }

And call it by:

[ array sortUsingFunction:order context:NULL];

And my array is sort that:

{1,9}
{1,6}
{1,5}
{2,2}
{0,18}
{12, 10}
{9,1}

Where is problem?

  • 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-05-30T01:59:58+00:00Added an answer on May 30, 2026 at 1:59 am

    You don’t say exactly what is wrong with the array after you sort it. I see two possible problems.

    1. As Eimantas says in his comment, you are sorting the array in reverse order (highest to lowest). If you want to sort lowest to highest, you need to say return [catA compare:catB].

    2. It looks like the elements of catA and catB are strings, not numbers, so you are sorting them as strings. The string ’10’ is less than the string ‘9’, but the number 10 is greater than the number 9. Even though you are casting the elements to NSNumber, that does not change the type of the underlying object, which is still NSString.

    You can sort them as numbers this way:

    [array sortUsingComparator:^(id a, id b) {
        return [[a lastObject] intValue] - [[b lastObject] intValue];
    }]
    

    But it might be better to convert the strings to number objects before sorting the array:

    for (NSMutableArray *element in array) {
        [element replaceObjectAtIndex:(element.count - 1)
            withObject:[NSNumber numberWithInt:[element.lastObject intValue]]];
    }
    
    [array sortUsingComparator:^(id a, id b) {
        return [[a lastObject] intValue] - [[b lastObject] intValue];
    }]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two arrays that I need to sort A->Z but all numerical indexes
I have a problem with how the List Sort method deals with sorting. Given
I have a 2D-array that I want to sort based on the second column.
I have a problem that I have been thinking about, but I can't really
can anyone help, i have problem doing a sort, I thought i had it
i have one problem when i sort the NSMutablearray using date and time wise.
I have sort of a tricky problem I'm attempting to solve. First of all,
I have some sort of a design problem with my Django AJAX application. I
I have problem in some JavaScript that I am writing where the Switch statement
I have combined two arrays using array_merge, and now need to sort them alphabetically.

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.