我有一个包含一些字符串的数组。对于字符串的每个字符,分配一个整数值。例如,a = 2,b = 5,c = 6,o = 1,k = 3等
字符串中的最终值是字符值的总和。因此,对于示例字符串“BOOK”,该字符串将被存储为“BOOK(7)”。类似地,每个字符串都有一个最终的整数值。我想用存储在每个数组索引中的字符串中的这些最终整数值对这些数组进行排序。该数组包含超过200,000个单词。所以排序过程应该非常快。有什么方法吗?
答案 0 :(得分:1)
一个残酷的快速示例可能是,如果您的字符串结构总是相同的,例如“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)”
)
答案 1 :(得分:0)
据我所知,您想要对包含以下
中格式化的字符串的数组进行排序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
答案 2 :(得分:0)
试试这个方法
+(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更改为您自己的类名;
答案 3 :(得分:0)
首先 - 创建一个自定义对象来保存您的值。不要将值放在字符串中。 排序不是您的基本问题。问题是您要将值保存到难以提取的字符串中。
@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:
。
请注意,这将超过性能中的所有其他答案,因为不需要在每次比较时解析该值。