ios 按字符串属性的第一个字母过滤数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18714634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Filter array by first letter of string property
提问by Nubaslon
I have an NSArray
with objects that have a name
property.
我有一个NSArray
带有name
属性的对象。
I would like filter the array by name
我想过滤数组 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)"
但是我收到一个错误 - “不能对不是字符串的东西进行子字符串操作 (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'.
我怎样才能做到这一点?我想过滤第一个字母name
为“A”的数组。
回答by iPatel
Try with following code
尝试使用以下代码
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF like %@", yourName];
NSArray *filteredArr = [yourArray filteredArrayUsingPredicate:pred];
EDITED :
编辑:
NSPredicate
pattern should be:
NSPredicate
模式应该是:
NSPredicate *pred =[NSPredicate predicateWithFormat:@"name beginswith[c] %@", alphabet];
回答by Arvind
Here is one of the basic use of NSPredicate for filtering array .
这是 NSPredicate 过滤数组的基本用法之一。
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);
回答by Thomas Keuleers
NSArray offers another selector for sorting arrays:
NSArray 提供了另一个用于排序数组的选择器:
NSArray *sortedArray = [array sortedArrayUsingComparator:^NSComparisonResult(Person *first, Person *second) {
return [first.name compare:second.name];
}];
回答by Sergey Demchenko
If you want to filterarray take a look on this code:
如果要过滤数组,请查看此代码:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@", @"qwe"];
NSArray *result = [self.categoryItems filteredArrayUsingPredicate:predicate];
But if you want to sortarray 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;
回答by SreeHarsha
use this
用这个
[listArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
回答by Jordi Puigdellívol
Checkout this library
签出这个库
https://github.com/BadChoice/Collection
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
这仅获取按字母顺序排序的以 A 开头的文本
If you are doing it with objects:
如果您使用对象执行此操作:
NSArray* result = [thArray filter:^BOOL(AnObject *object) {
return [[object.name substr:0] isEqualToString:@"A"];
}] sort:@"name"];