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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:16:50+00:00 2026-06-01T09:16:50+00:00

When I sort the array using sortedArrayUsingSelector method , all works fine except it

  • 0

When I sort the array using sortedArrayUsingSelector method , all works fine except it brings records with BLANK names at the top of array . I don’t want records with BLANK names on top of array instead they should be last in the array. How can I implement this? Here is my code

self.tempArray = [NSMutableArray arrayWithArray:[self.tempArray sortedArrayUsingSelector:@selector(compare:)]];

I am sorting the array on first name of user. Here is code of compare method

    -(NSComparisonResult)compare:(User*)user {

    return [self.firstName compare:user.firstName];
}

Any kind of help is highly appreciated. Thanks.

  • 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-01T09:16:52+00:00Added an answer on June 1, 2026 at 9:16 am

    Change your comparison method so that you treat empty names differently.

    -(NSComparisonResult)compare:(User*)user 
    { 
        NSString *selfName;
        NSString *userName;
        if (self.firstName && ![self.firstName isEqualToString:@""])
            selfName = self.firstName;
        else
            selfName = @"zzzzzzzzzz";
    
        if (user.firstName && ![user.firstName isEqualToString:@""])
            userName = user.firstName;
        else
            userName = @"zzzzzzzzzz";
    
        return [selfName localizedCaseInsensitiveCompare:userName]; 
    }
    

    You can probably write this in fewer lines of code but I have left it like this for readability. nil cannot be used as a comparator for string comparisons.

    Currently it is (correctly) sorting blanks to the top, since they are come before populated strings in an alphanumeric sort.

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

Sidebar

Related Questions

When I sort an Array using the native sort method, which algorithm does Ruby
I want to sort array using Bubble Sort. I an missing a small point
is there a way in Scala to sort an array of tuples using and
I have to sort an array of 10 numbers using insertion sort and display
I have an array that I would like to sort using a date field
Is there a better way to shell sort using C#? // array of integers
I'm trying to pre-sort an array of items are added into an array using
I want to sort array of random numbers using MPI library. The idea is
Is there a way to sort an array using Chrome? Using the sort function
I know how to sort-in-place an array using heapsort and max-heap property. But I

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.