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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:49:20+00:00 2026-06-13T11:49:20+00:00

I am trying to sort an iOS UITableView object. I am currently using the

  • 0

I am trying to sort an iOS UITableView object. I am currently using the following code:

// Sort terms alphabetically, ignoring case
[self.termsList sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

This sorts my list, whist ignoring case. However, it would be nice to ignore punctuation as well. For example:

c.a.t.
car
cat

should be sorted as follows:

car
c.a.t.
cat

(It doesn’t actually matter which of the two cats (cat or c.a.t.) comes first, so long as they’re sorted next to one another).

Is there a simple method to get around this? I presume the solution would involve extracting JUST the alphanumeric characters from the strings, then comparing those, then returning them back to their former states with the non-alphanumeric characters included again.

In point of fact, the only characters I truly care about are periods (.) but if there is a solution that covers all punctuation easily then it’d be useful to know.

Note: I asked this exact same question of Java a month ago. Now, I am creating the same solution in Objective-C. I wonder if there are any tricks available for the iOS API that make this easy…

Edit: I have tried using the following code to strip punctuation and populate another array which I sort (suggested by @tiguero). However, I don’t know how to do the last step: to actually sort the first array according to the order of the second. Here is my code:

    NSMutableArray *arrayWithoutPunctuation = [[NSMutableArray alloc] init];
    for (NSString *item in arrayWithPunctuation)
    {
        // Replace hyphens/periods with spaces
        item = [item stringByReplacingOccurrencesOfString:@"-" withString:@" "]; // ...hyphens
        item = [item stringByReplacingOccurrencesOfString:@"." withString:@" "]; // ...periods
        [arrayWithoutPunctuation addObject:item];
    }
    [arrayWithoutPunctuation sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

This provides ‘arrayWithoutPunctuation’ which is sorted, but of course doesn’t contain the punctuation. This is no good, since, although it is now sorted nicely, it no longer contains punctuation which is crucial to the array in the first place. What I need to do is sort ‘arrayWithPunctuation’ according to the order of ‘arrayWithoutPunctuation’… Any help appreciated.

  • 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-13T11:49:21+00:00Added an answer on June 13, 2026 at 11:49 am

    You can use a comparison block on an NSArray and your code will look like the following:

    NSArray* yourStringList = [NSArray arrayWithObjects:@"c.a.t.", @"car", @"cat", nil];
    NSArray* yourStringSorted = [yourStringList sortedArrayUsingComparator:^(id a, id b){
            NSString* as = (NSString*)a;
            NSString* bs = (NSString*)b;
    
            NSCharacterSet *unwantedChars = [NSCharacterSet characterSetWithCharactersInString:@"\\.:',"];
            //Remove unwanted chars
            as = [[as componentsSeparatedByCharactersInSet: unwantedChars] componentsJoinedByString: @""];
            bs = [[as componentsSeparatedByCharactersInSet: unwantedChars] componentsJoinedByString: @""];
    
            // make the case insensitive comparison btw your two strings
            return [as caseInsensitiveCompare: bs];
        }];
    

    This might not be the most efficient code actually one other option would be to iterate on your array first and remove all unwanted chars and use a selector with the caseInsensitiveCompare method:

     NSString* yourStringSorted = [yourStringList sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use the following code to handle SSL connections in iOS. It
I'm trying to sort the employees object array by age and i'm getting the
I am trying to sort a Vector list using the Java Collections frameworks. This
I'm trying to sort a list of objects using my_list.sort(key=operator.attrgetter(attr_name)) but if any of
I'm trying to sort an pointer array using pointers instead of indexes but I'm
I am trying to sort an array of integers in MIPS using bubble sort
I am trying to sort a list of records that have been created using
Trying to sort an array by writing my own sort method using recursion (Pine's
I'm setting up a new store using 1.4.1 I'm trying sort the products in
I'm trying to incorporate some sort of error handling into an iOS app. After

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.