我有一个包含一些字符串的数组。为字符串的每个字符分配一个整数值。例如 a=2,b=5,c=6 ,o=1,k=3 等
a 字符串中的最终值是字符值的总和。因此,对于示例字符串“BOOK”,该字符串将存储为“BOOK (7)”。同样,每个字符串都有一个最终的整数值。我想用存储在每个数组索引中的字符串中的这些最终整数值对这些数组进行排序。该数组包含超过 200,000 个单词。所以排序过程应该很快。有什么方法吗?
一个残酷的快速示例可能是,如果您的字符串结构始终相同,例如“Book (7)”,您可以通过查找“()”之间的数字对字符串进行操作,然后您可以使用字典临时存储对象:
NSMutableArray *arr=[NSMutableArray arrayWithObjects:@"Book (99)",@"Pencil (66)",@"Trash (04)", nil];
NSLog(@"%@",arr);
NSMutableDictionary *dict=[NSMutableDictionary dictionary];
//Find the numbers and store each element in the dictionary
for (int i =0;i<arr.count;i++) {
NSString *s=[arr objectAtIndex:i];
int start=[s rangeOfString:@"("].location;
NSString *sub1=[s substringFromIndex:start];
NSString *temp1=[sub1 stringByReplacingOccurrencesOfString:@"(" withString:@""];
NSString *newIndex=[temp1 stringByReplacingOccurrencesOfString:@")" withString:@""];
//NSLog(@"%d",[newIndex intValue]);
[dict setValue:s forKey:newIndex];
}
//Sorting the keys and create the new array
NSArray *sortedValues = [[dict allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSMutableArray *newArray=[[NSMutableArray alloc]init];
for(NSString *valor in sortedValues){
[newArray addObject:[dict valueForKey:valor]];
}
NSLog(@"%@",newArray);
这打印:
(
“书(99)”,
“铅笔(66)”,
“垃圾(04)”
)(
“垃圾(04)”,
“铅笔(66)”,
“书(99)”
)
据我了解,您想对包含以下格式的字符串的数组进行排序
a=3
并且您想在忽略字符的同时根据数字进行排序。在这种情况下,以下代码将适用于您
-(NSArray *)Sort:(NSArray*)myArray
{
return [myArray sortedArrayUsingComparator:(NSComparator)^(id obj1, id obj2)
{
NSString *first = [[obj1 componentsSeparatedByString:@"="] objectAtIndex:1];
NSString *second = [[obj2 componentsSeparatedByString:@"="] objectAtIndex:1];
return [first caseInsensitiveCompare:second];
}];
}
如何使用它:
NSArray *arr= [[NSArray alloc] initWithObjects:@"a=3",@"b=1",@"c=4",@"f=2", nil];
NSArray *sorted = [self Sort:arr];
for (NSString* str in sorted)
{
NSLog(@"%@",str);
}
输出
b=1
f=2
a=3
c=4
试试这个方法
+(NSString*)strTotalCount:(NSString*)str
{
NSInteger totalCount = 0;
// initial your character-count directory
NSDictionary* characterDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:2], [NSString stringWithUTF8String:"a"],
[NSNumber numberWithInt:5], [NSString stringWithUTF8String:"b"],
[NSNumber numberWithInt:6], [NSString stringWithUTF8String:"c"],
[NSNumber numberWithInt:1], [NSString stringWithUTF8String:"o"],
[NSNumber numberWithInt:3], [NSString stringWithUTF8String:"k"],
nil];
NSString* tempString = str;
for (NSInteger i =0; i<tempString.length; i++) {
NSString* character = [tempString substringWithRange:NSMakeRange(i, 1)];
character = [character lowercaseString];
NSNumber* count = [characterDictionary objectForKey:character];
totalCount += [count integerValue];
};
return [NSString stringWithFormat:@"%@(%d)",str,totalCount];
}
测试语句:
NSLog(@"%@", [ViewController strTotalCount:@"BOOK"]);
将输出“ BOOK(10) ”
您可以将 ViewController 更改为您自己的类名;
首先 - 创建一个自定义对象来保存您的值。不要将值放在字符串中。排序不是你的基本问题。问题是您将值保存到难以提取的字符串中。
@interface StringWithValue
@property (nonatomic, copy, readwrite) NSString* text;
@property (nonatomic, assign, readwrite) NSUInteger value;
- (id)initWithText:(NSString*)text;
- (NSComparisonResult)compare:(StringWithValue*)anotherString;
@end
@implementation StringWithValue
@synthesize text = _text;
@synthesize value = _value;
- (id)initWithText:(NSString*)text {
self = [super init];
if (!self) {
return nil;
}
self.text = text;
self.value = [self calculateValueForText:text];
return self;
}
- (NSComparisonResult)compare:(StringWithValue*)anotherString {
if (self.value anotherString.value) {
return NSOrderedDescending;
}
else {
return NSOrderedSame;
}
}
- (NSString*)description {
return [NSString stringWithFormat:@"%@ (%u)", self.text, self.value];
}
@end
然后对数组进行排序将是sortUsingSelector:
. 请注意,这将在性能上击败所有其他答案,因为无需在每次比较时解析值。