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

The Archive Base Latest Questions

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

I was having some confusion when the book started talking about alphabetizing elements in

  • 0

I was having some confusion when the book started talking about alphabetizing elements in the array using the sort and compareNames: method. How is the argument for compareNames obtained when the method sort is called? And how do the elements actually get alphabetize when all that’s returned is a type of NSComparisonResult?

#import <Foundation/Foundation.h>

@interface AddressCard : NSObject
@property (copy, nonatomic) NSString *name, *email;

-(NSComparisonResult) compareNames: (id) element;

@end

@implementation AddressCard 
@synthesize name, email;

-(NSComparisonResult) compareNames:(id)element {
    return [name compare: [element name]];
}
@end

#import "AddressCard.h"
#import <Foundation/Foundation.h>

@interface AddressBook : NSObject
@property (nonatomic, copy) NSString *bookName;
@property (nonatomic, strong) NSMutableArray *book;

-(void) addCard: (AddressCard *) myCard;
-(void) sort;

@end

@implementation AddressBook
@synthesize book, bookName;

-(void) sort {
    [book sortUsingSelector: @selector(compareNames:)];
}
  • 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-08T18:52:45+00:00Added an answer on June 8, 2026 at 6:52 pm

    A NSComparisonResult is just one of NSOrderedAscending, NSOrderedSame, NSOrderedDescending.

    The method compareNames is responsible of the true comparison. In this case, since you are comparing strings, the method just rely on already-implemented compare method of NSString. The result gives information about the comparison between two strings according to the alphabetical order.

    Method sortUsingSelector of NSMutableArray is a useful method to sort custom types of data (you can appreciate better if you try to sort custom objects according to custom criteria). It accepts a selector, which means that every time it needs to compare two object, the method specified by the selector is called.

    For what I remember you are not allowed to know the method sortUsingSelector internally uses (quick sort / heap sort / bubble sort…), what you need to know is that object are ordered using the criteria specified by the implementation of the method you pass with the selector.

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

Sidebar

Related Questions

I'm having some real confusion about events in c#... if I have this code
I am having some confusion with the new keyword,things work fine when I am
ASP.NET Application I am having some confusion with showing images. IE is fully working
Having some issues with the ... in ObjectiveC. I'm basically wrapping a method and
I am having some confusion with a mysql query. Any help appreciated. My current
I still have some confusion about this thing. What I have found till now
I'm having some confusion if a VOIP App can have multiple tcp sockets monitored
Using SQL Server 2008... I'm having some troubles in trying to order my rows
I am having some issues surrounding the calling of a generic method. In the
Having some out-of-memory problems with a 32-bit process in Windows I begun using Performance

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.