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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:13:54+00:00 2026-05-14T20:13:54+00:00

I have an array named ‘names’ with strings looking like this: [name_23_something, name_25_something, name_2_something];

  • 0

I have an array named ‘names’ with strings looking like this:
[“name_23_something”, “name_25_something”, “name_2_something”];
Now I would like to sort this array in ascending order so it looks like this:
[“name_25_something”, “name_23_something”, “name_2_something”];

I guess that should start of with extracting the numbers since I want that
the sorting is done by them:

for(NSString *name in arr) {
    NSArray *nameSegments = [name componentsSeparatedByString:@"_"];
    NSLog("number: %@", (NSString*)[nameSegments objectAtIndex:1]);     
}

I’m thinking of creating a dictionary with the keys but I’m not sure if that is the correct objective-c way, maybe there some some methods I could use instead? Could you please me with some tips or example code how this sorting should be done in a proper way.

Thank you

  • 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-14T20:13:55+00:00Added an answer on May 14, 2026 at 8:13 pm
    @implementation NSString (MySort)
    
    - (int) myValue {
        NSArray *nameSegments = [self componentsSeparatedByString:@"_"];
        return [[nameSegments objectAtIndex:1] intValue];
    }
    
    - (NSComparisonResult) myCompare:(NSString *)other {
        int result = [self myValue] - [other myValue];
        return result < 0 ? NSOrderedAscending : result > 0 ? NSOrderedDescending : NSOrderedSame;
    }
    
    @end
    
    ...
    
    arr = [arr sortedArrayUsingSelector:@selector(myCompare:)];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have mutable string array named arrayout. It is having 3 element .Now I
I have a string array in C named args[] - now how can I
I have this array named $clientinfo : [id] => 1 [dlastname] => Brown [dfirstname]
I have this method to save a mutable array named myWallet that contains instances
I have an array named Floors in class A, it contains values something like
I have an array defined in my strings.xml file names. I want the values
I have a javascript function that returns an array. I would like to know
I have an array list named newSymptomList which contains a list of Symptom id's
In my Values folder for my project, I have a string array named currency_avatar.
I have an array which contains sets of three similar named items; however, sometimes

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.