16

I have an NSArray with objects that have a name property.

I would like filter the 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 I get an 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 the array for the first letter in name being 'A'.

jscs
  • 62,161
  • 12
  • 145
  • 186
Nubaslon
  • 631
  • 1
  • 9
  • 27
  • possible duplicate of [How to sort an NSMutableArray with custom objects in it?](http://stackoverflow.com/questions/805547/how-to-sort-an-nsmutablearray-with-custom-objects-in-it) – Martin R Sep 10 '13 at 09:13
  • No, i would like to filter by first letter - "A" – Nubaslon Sep 10 '13 at 09: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 '13 at 09:22

6 Answers6

24

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];
iPatel
  • 41,165
  • 13
  • 109
  • 131
  • Not working( Error - Can't do a substring operation with something that isn't a string (lhs = <1, Arraow> rhs = А) – Nubaslon Sep 10 '13 at 09:20
  • @please put your array list and explain me what you want ? – iPatel Sep 10 '13 at 09:23
  • @Nubaslon- change NSPredicate pattern such like `SELF beginswith[c] %@` – iPatel Sep 10 '13 at 09:24
  • Array contains object of class Town, Town has property - id, name – Nubaslon Sep 10 '13 at 09:29
  • Then what is `NSLog(@"%@",listSimpl);` – iPatel Sep 10 '13 at 09:32
  • <1, Москва, 55.755772, 37.617760, 0> <762, Russia, 48.736416, 44.493610, 0> <2000000307, Tel Aviv, 50.929810, 6.955242, 0> <2000001662, Abakan, 53.720978, 91.442421, 0> <2000002332, Azov, 47.092899, 39.426750, 0> <12029, Actobe, 50.300411, 57.154591, 0> <970, Berkut, 56.392387, 38.726463, 0> <798, Bolinsk, 43.238274, 76.945465, 0> <2000001633, Bolshevik, 54.900482, 52.306652, 0> <312, Vilnus, 44.891445, 37.321228, 0> – Nubaslon Sep 10 '13 at 09:35
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/37087/discussion-between-nubaslon-and-ipatel) – Nubaslon Sep 10 '13 at 09:38
10

Here is one of the basic use of NSPredicate for filtering array .

NSMutableArray *array =
[NSMutableArray arrayWithObjects:@"Nick", @"Ben", @"Adam", @"Melissa", @"arbind", nil];

NSPredicate *sPredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] 'b'"];
NSArray *beginWithB = [array filteredArrayUsingPredicate:sPredicate];
NSLog(@"beginwithB = %@",beginWithB);
Arvind
  • 450
  • 1
  • 4
  • 11
3

NSArray offers another selector for sorting arrays:

NSArray *sortedArray = [array sortedArrayUsingComparator:^NSComparisonResult(Person *first, Person *second) {
    return [first.name compare:second.name];
}];
Thomas Keuleers
  • 5,895
  • 2
  • 30
  • 35
1

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;
Sergey Demchenko
  • 2,804
  • 1
  • 22
  • 24
0

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

use this

[listArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
codercat
  • 21,439
  • 9
  • 56
  • 84
SreeHarsha
  • 416
  • 4
  • 17
0

Checkout this library

https://github.com/BadChoice/Collection

It comes with lots of easy array functions to never write a loop again

So you can just do

NSArray* result = [thArray filter:^BOOL(NSString *text) {
    return [[name substr:0] isEqualToString:@"A"]; 
}] sort];

This gets only the texts that start with A sorted alphabetically

If you are doing it with objects:

NSArray* result = [thArray filter:^BOOL(AnObject *object) {
    return [[object.name substr:0] isEqualToString:@"A"]; 
}] sort:@"name"];
Jordi Puigdellívol
  • 1,436
  • 2
  • 20
  • 28