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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:41:54+00:00 2026-06-17T14:41:54+00:00

I need to sort a NSMutableArray containing NSURLs with localizedStandardCompare: [array sortUsingComparator:^NSComparisonResult(id obj1, id

  • 0

I need to sort a NSMutableArray containing NSURLs with localizedStandardCompare:

[array sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    NSString *f1 = [(NSURL *)obj1 absoluteString];
    NSString *f2 = [(NSURL *)obj2 absoluteString];
    return [f1 localizedStandardCompare:f2];
}];

This works fine, but I worry a bit about the performance: the block will be evaluated n log n times during the sort, so I’d like it to be fast (the array might have up to 100,000 elements). Since localizedStandardCompare is only available on NSString, I need to convert the URLs to strings. Above, I use absoluteString, but there are other methods that return a NSString, for example relativeString. Reading the NSURL class reference, I get the impression that relativeString might be faster, since the URL does not need to be resolved, but this is my first time with Cocoa and OS-X, and thus just a wild guess.

Additional constraint: in this case, all URLs come from a NSDirectoryEnumerator on local storage, so all are file URLs. It would be a bonus if the method would work for all kinds of URL, though.

My question: which method should I use to convert NSURL to NSString for best performance?

Profiling all possible methods might be possible, but I have only one (rather fast) OS-X machine, and who knows – one day the code might end up on iOS.

I’m using Xcode 4.5.2 on OS-X 10.8.2, but the program should work on older version, too (within reasonable bounds).

  • 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-17T14:41:56+00:00Added an answer on June 17, 2026 at 2:41 pm

    I benchmarked the sort. It turned out that absoluteString and relativeString are much faster that path or relativePath.

    Sorting about 26000 entries:

    relativeString      550ms
    absoluteString      580ms
    path                920ms
    relativePath        960ms
    field access        480ms
    

    For field access, I put the value of absoluteString into a field prior to the sort and access that. So, the ...String accessors are almost as fast as field access, and thus a good choice for my use case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need so sort an array with an array inside, something like this: NSMutableArray
I have a NSMutableArray consisting out of NSString s. When I need to sort
I need to sort an array that can look like this: $array[4][0] = array('id'
I need to sort an array of months (alphabetical months) according to their months
I have a NSMutableArray and I need to sort its elements into separate C
I need reverse(sort in descending order) a NSMutablearray.I tried the following code and it
I need to sort this array by the subsubkey description ascending. I tried a
I need to sort an nsmutablearray of nsdictionaries, using a keyed object from the
I need to sort a flat file by third column leaving first column intact
I need to sort list of strings in the alphabetical order: List<String> list =

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.