我有一个包含一些字符串的数组。对于字符串的每个字符,都会分配一个整数值。例如a = 2,b = 5,c = 6,o = 1,k = 3等用计数值对数组排序
字符串中的最终值是字符值的总和。因此,对于字符串“BOOK”的例子,字符串将被存储为“BOOK(7)”。同样,每个字符串都会有一个最终的整数值。我想用存储在每个数组索引中存在的字符串中的这些最终整数值排序这些数组。该数组包含超过200,000个字。所以排序过程应该非常快。有没有办法呢?
我有一个包含一些字符串的数组。对于字符串的每个字符,都会分配一个整数值。例如a = 2,b = 5,c = 6,o = 1,k = 3等用计数值对数组排序
字符串中的最终值是字符值的总和。因此,对于字符串“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)”
你自己的类名您可以更改视图控制器;
这真的让我发笑。你吸了什么? – Till
首先 - 创建自定义对象到保存你的价值。不要把值放在字符串中。排序不是你的基本问题。问题在于你将值从一个字符串中提取出来难以提取。
@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:
。 请注意,这将在性能上超过所有其他答案,因为不需要每次比较都解析该值。
我认为你应该使用整数部分的正则表达式,然后你可以排序数据 –
我怎么能在这种情况下使用正则表达式? – Kiron
你的数组是这样的:[“book5”,“table3”,“pen2”]? – sunkehappy