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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:43:12+00:00 2026-06-08T14:43:12+00:00

I get an array of strings with the following strings, there is a certain

  • 0

I get an array of strings with the following strings, there is a certain patern to the strings

str1
str51
str3
str4
str10
str39
str31
str191

Every string starts with ‘str’ and has a number appended onto the end of it.

Is there a neath way to sort the array so it lists the strings in order of value

str1
str3
str4
str10
str31
str39
str51
str191

I can see a way to do it by writing some recursive function that will

NSString * firstString = [[strArray objectAtIndex:i].substringFromIndex:3];
NSString * secondString = [[strArray objectAtIndex:i+1].substringFromIndex:3];

if ([firstString intValue] < [secondString intValue])
{
    //do nothing they order is correct
}
else
{
    //swap the order of the 2 strings in the array
}

But thats very rudimentary code, Is there some mechanism in Objective-C or a nice code trick to handle this sorting better?

Many Thanks,
-Code

  • 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-08T14:43:14+00:00Added an answer on June 8, 2026 at 2:43 pm

    If they all start with the same prefix, I believe that the default sort will handle them appropriately. By this, I mean:

    [someArray sortUsingSelector:@selector(compare:)];
    

    If, for some reason, this isn’t working, then you can use a block. Try this:

    NSArray *sortedArray = [someArray sortedArrayUsingComparator:^NSComparisonResult(id a, id b) 
    {
        //Default compare, to protect against cast
        if (![a isKindOfClass:[NSString class]] || ![b isKindOfClass:[NSString class]]) {
            return ([a compare:b]);
        }
        else {
            NSString *aString = (NSString*) a;
            NSString *bString = (NSString*) b;
            int aInt = [[a.substringFromIndex:3] intValue];
            int bInt = [[b.substringFromIndex:3] intValue];
            return [aInt < bInt];
        }
    }];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following string fromat: session=11;reserID=1000001 How to get string array of number?
I wonder how to get a string (or array of strings) between two known
System.out.println(ganzeZeile[26]); System.out.println(filter.get(11)); System.out.println(ganzeZeile[26].contains(filter.get(11))); ganzeZeile is an array of Strings. filter is an ArrayList of
Is there a easy way to get an array out of a string (html)
I have an array of strings. Not sure if there is simple way to
If I have the following array of strings: string[] stringArray = {"one", "two", "three",
I have an array of values, all strings. There are also 2 marker entries
I have the following code function generate_pdf() { $fdf_data_strings = $this->get_hash_for_pdf(); #$fdf_data_names = array('49a'
I need to get string array or list with ajax and Action, this is
I'm trying to build up a string array in JavaScript and get the results

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.