Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have NSArray *listArray with objects like - (id, name).

I would like sort array by (name) -

    NSString *alphabet = [agencyIndex objectAtIndex:indexPath.section];
    //---get all states beginning with the letter---
    NSPredicate *predicate =
    [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alphabet];
    NSMutableArray *listSimpl = [[NSMutableArray alloc] init];
    for (int i=0; i<[[Database sharedDatabase].agents count]; i++) {
        Town *_town = [[Database sharedDatabase].agents objectAtIndex:i];
        [listSimpl addObject:_town];
    }
    NSArray *states = [listSimpl filteredArrayUsingPredicate:predicate];

But error - "Can't do a substring operation with something that isn't a string (lhs = <1, Arrow> rhs = A)"

How can i do this? I would like to filter Array with object by first letter in (name)- "A"

share|improve this question
 
possible duplicate of How to sort an NSMutableArray with custom objects in it? –  Martin R Sep 10 at 9:13
 
No, i would like to filter by first letter - "A" –  Nubaslon Sep 10 at 9:18
 
The most important piece of code for this post is the predicate itselft but was left out. Show how you defined the predicate? –  user523234 Sep 10 at 9:22

4 Answers

up vote 0 down vote accepted

Try with following code

NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF like %@", yourName];
NSArray *filteredArr = [yourArray filteredArrayUsingPredicate:pred];

EDITED :

NSPredicate pattern should be:

NSPredicate *pred =[NSPredicate predicateWithFormat:@"name beginswith[c] %@", alphabet];
share|improve this answer
 
Not working( Error - Can't do a substring operation with something that isn't a string (lhs = <1, Arraow> rhs = А) –  Nubaslon Sep 10 at 9:20
 
@please put your array list and explain me what you want ? –  iPatel Sep 10 at 9:23
 
@Nubaslon- change NSPredicate pattern such like SELF beginswith[c] %@ –  iPatel Sep 10 at 9:24
 
i already have it –  Nubaslon Sep 10 at 9:26
 
Array contains object of class Town, Town has property - id, name –  Nubaslon Sep 10 at 9:29
show 3 more comments

NSArray offers another selector for sorting arrays:

NSArray *sortedArray = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    Person *first = (Person *) obj1;
    Person *second = (Person *) obj2; 

    return [first.name compare:second.name];
}];
share|improve this answer

visit https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Collections/Articles/Arrays.html

use this [listArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

share|improve this answer

If you want to filter array take a look on this code:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@", @"qwe"];
NSArray *result = [self.categoryItems filteredArrayUsingPredicate:predicate];

But if you want to sort array take a look on the following functions:

- (NSArray *)sortedArrayUsingFunction:(NSInteger (*)(id, id, void *))comparator context:(void *)context;
- (NSArray *)sortedArrayUsingFunction:(NSInteger (*)(id, id, void *))comparator context:(void *)context hint:(NSData *)hint;
- (NSArray *)sortedArrayUsingSelector:(SEL)comparator;
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.