我需要在表格视图中对字母数据进行排序并忽略数字,例如:
- 123 test
- 939 urne
- 403 vase
目前,数据加载如下:
- 939 urne
- 123 test
- 403 vase
忽略数字的最佳选择是什么?
答案 0 :(得分:1)
您可以将数据分别存储在词典中,例如
Array
(
{
"number" = 403
"text" = "vase"
},
{
"number" = 403
"text" = "vase"
}
)
然后你可以使用
排序NSSortDescriptor * descriptor = [NSSortDescriptor sortDescriptorWithKey:@"text" ascending:YES];
[array sortUsingDescriptors:[NSArray arrayWithObject:descriptor]];
答案 1 :(得分:0)
最佳途径
NSSortDescriptor *descriptor; = [NSSortDescriptor sortDescriptorWithKey:@"text" ascending:YES comparator:^(NSString *obj1, NSString *obj2) {
return [obj1 compare:obj2 :NSCaseInsensitiveSearch];
}];
[arrDocuments sortUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]];
答案 2 :(得分:0)
现在启动一个循环,其中包含您要排序的数组中的所有对象。假设数组的名称是originalArray
。
NSMutableArray *newArray = [[NSMutableArray alloc] init];
NSMutableArray *finalArray = [[NSMutableArray alloc] init];
for(int i = 0; i < originalArray.count; i++)
{
NSString *tempstr = [originalArray objectAtIndex:i]; // suppose it is 123 - abcd
NSString *newString = [[tempstr componentsSeparatedByCharactersInSet: [[NSCharacterSet letterCharacterSet] invertedSet]] componentsJoinedByString:@""];
NSDictionary *dict = [[NSDictionary alloc] init];
[dict setValue:@(i) forKey:@"num"];
[dict setValue:newString forKey:@"char"];
[newArray addObject:dict];
}
现在您可以对newArray
进行排序: -
NSSortDescriptor * descriptor = [NSSortDescriptor sortDescriptorWithKey:@"char" ascending:YES];
[newArray sortUsingDescriptors:[NSArray arrayWithObject:descriptor]];
现在您可以从此数组中的字典中获取索引(在 num 键下)
for(int j = 0; j<newArray.count ; j++)
{
NSDictionary *dict1 = [[NSDictionary alloc] init];
dict1 = [newArray objectAtIndex: j];
int number = [[dict1 objectForKey:@"num"] intValue];
NSString *str = [originalArray objectAtIndex:number];
//Now add this string in the finalArray.
[finalArray addObject:str];
}
我希望你能理解这一点。 :)