ViruMax
ViruMax

Reputation: 1218

sort nsarray by second word. (by surname in "firstname surname" kind of strings array)

I am having nsarray of strings like "fistname surname". Now I want to sort that strings array by second word i.e. by surname. I used this method

NSArray *sortedArray=[[teachersList allValues] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
    NSLog(@"After %@",sortedArray);

But it sorts array on first word.

And i am also having elements or strings with firstname only means only first word.

So how to do this?

Edit

@Janak Nirmal

My modified code is:

while([resultSet next])
{
    [teachersList setObject:[NSString stringWithFormat:@"%@ %@",[resultSet stringForColumn:@"first_name"],[resultSet stringForColumn:@"last_name"]] forKey:[resultSet stringForColumn:@"email_id"]];
    [emailList addObject:[resultSet stringForColumn:@"email_id"]];
    appDelegate.grade=[resultSet stringForColumn:@"grade"];

}

    //NSArray *sortedArrays=[[teachersList allValues] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];


   NSArray *sortedArray = [[teachersList allValues] sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
        NSString *firstTeacher2ndWord = [[[[(Teacher*)a name] componentsSeparatedByString:@" "] objectAtIndex:1] lowercaseString];
        NSString *secondTeacher2ndWord = [[[[(Teacher*)b name] componentsSeparatedByString:@" "] objectAtIndex:1] lowercaseString];
        return [firstTeacher2ndWord compare:secondTeacher2ndWord];
    }];

but i m getting following error

[__NSCFString name]: unrecognized selector sent to instance

Upvotes: 4

Views: 701

Answers (2)

Janak Nirmal
Janak Nirmal

Reputation: 22726

You can do sort like,

NSArray *sortedArray = [teacherList sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
    NSString *firstTeacher2ndWord = [[[[(Teacher*)a name] componentsSeparatedByString:@" "] objectAtIndex:1] lowercaseString];
    NSString *secondTeacher2ndWord = [[[[(Teacher*)b name] componentsSeparatedByString:@" "] objectAtIndex:1] lowercaseString];
    return [firstTeacher2ndWord compare:secondTeacher2ndWord];
}];
//Assuming in above you have atleast 2 words in name 
//i.e. objectAtIndex:1 doesn't fail or you need to put condition 
//there according to your needs

Assuming your model structure as below,

@interface Teacher : NSObject

@property (nonatomic,strong) NSString *name;

@end

EDIT

As your [teacherList allValues] would be directly holding string values you should write it as below

NSArray *sortedArray = [[teachersList allValues] sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
    NSString *firstTeacher2ndWord = [[[((NSString*)a) componentsSeparatedByString:@" "] objectAtIndex:1] lowercaseString];
    NSString *secondTeacher2ndWord = [[[((NSString*)b) componentsSeparatedByString:@" "] objectAtIndex:1] lowercaseString];
    return [firstTeacher2ndWord compare:secondTeacher2ndWord];
}];

Above code will give you array of all values directly in sorted manner.

Upvotes: 4

Retro
Retro

Reputation: 4005

because you have single string and it will always start to compare from "first name"

so you have to first split the string by space

NSArray *listItems = [list componentsSeparatedByString:@" "];

from here you have to build a dictionary of name with this split string like

@{@"FirstName" : listItems[0], "FirstName" : listItems[1]}

and add into array and then you can sort this

NSArray *keys = [theDictionary allKeys];
NSArray *sortedKeys = [keys sortedArrayUsingSelector:@selector(compareMethod:)];

Upvotes: 2

Related Questions