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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:11:24+00:00 2026-05-26T06:11:24+00:00

I have code that returns an NSComparisonResult. My codes works great for sorting my

  • 0

I have code that returns an NSComparisonResult. My codes works great for sorting my arrays in Ascending and Descending order when they are NSStrings with only alphabetic characters in them.

When the NSStrings have numbers in them, such as Seinfeld (Season 5) and Seinfeld (Season 6) they are sorting correctly Ascending in the order of 5 and then 6. But Descending it still sorts it as 5 and 6. Here’s an example:

Ascending:
Batman,
Seinfeld (Season 5),
Seinfeld (Season 6),
Zoolander

Descending:
Zoolander,
Seinfeld (Season 5),
Seinfeld (Season 6),
Batman

The descending code I have looks like this to return the comparison:

return [(NSString *)secondString compare:(NSString *)firstString options:NSNumericSearch];

Any ideas on how to reverse NSStrings with numbers in them?

By the way, I can’t use NSSortDescriptor in my case. The code I have requires a custom function to be built because I have to do some string appending, etc, before the sort occurs. I didn’t include it all for simplicity.

  • 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-26T06:11:25+00:00Added an answer on May 26, 2026 at 6:11 am

    Essentially the same comment as above, but here’s full source and output to prove it works (iOS 4.3 simulator tested):

    NSArray * strings = [NSArray arrayWithObjects: @"Seinfeld (Season 6)", @"Batman", @"Seinfeld (Season 5)", @"Zoolander", nil];
    NSArray * sorted1 = [strings sortedArrayUsingComparator: ^(id first, id second) {
                            return [(NSString *)first compare:(NSString *)second options: NSNumericSearch];
                        }];
    NSArray * sorted2 = [strings sortedArrayUsingComparator: ^(id first, id second) {
        return - [(NSString *)first compare:(NSString *)second options: NSNumericSearch];
        // or (to be more clear, but less efficient)
        // return -1 * [(NSString *)first compare:(NSString *)second options: NSNumericSearch];
    }];
    
    NSLog(@"starting: %@", strings );
    NSLog(@"sort1: %@", sorted1 );
    NSLog(@"sort2: %@", sorted2 );
    

    output:

    2011-10-19 02:09:07.557 test2222[16793:207] starting: (
      "Seinfeld (Season 6)",
      Batman,
      "Seinfeld (Season 5)",
      Zoolander
    )
    
    2011-10-19 02:09:07.558 test2222[16793:207] sort1: (
      Batman,
      "Seinfeld (Season 5)",
      "Seinfeld (Season 6)",
      Zoolander
    )
    
    2011-10-19 02:09:07.558 test2222[16793:207] sort2: (
      Zoolander,
      "Seinfeld (Season 6)",
      "Seinfeld (Season 5)",
      Batman
    ) 
    

    In answer to Ethan Allen, here’s the output for a test with numbers in front:

    2011-12-14 21:01:26.535 Test[7475:f803] starting: (
        "2001 Seinfeld (Season 6)",
        "1999 Batman",
        "2112 Seinfeld (Season 5)",
        "10 Zoolander"
    )
    2011-12-14 21:01:26.537 Test[7475:f803] sort1: (
        "10 Zoolander",
        "1999 Batman",
        "2001 Seinfeld (Season 6)",
        "2112 Seinfeld (Season 5)"
    )
    2011-12-14 21:01:26.537 Test[7475:f803] sort2: (
        "2112 Seinfeld (Season 5)",
        "2001 Seinfeld (Season 6)",
        "1999 Batman",
        "10 Zoolander"
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following vbscript code that returns the local IP address. It works
I have a method in my Python code that returns a tuple - a
Suppose we have the xhr 'POST' request that returns 'redirect' status code. In that
I have code that looks like the following, which works fine for displaying the
I have some code that returns unique elements in my dictionary, but I would
I have the following code that returns a reference to an array with one
Lets say I have the following code that returns number of anchor elements on
I have the following code that returns at the end of some of my
I have the following code in a WCF Service that returns a string, and
I have some code that returns a struct containing 2 objects (declared as id

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.