Iphone 使用计数值对数组进行排序
我有一个数组,其中包含一些字符串。为字符串的每个字符分配一个整数值。例如a=2,b=5,c=6,o=1,k=3等等Iphone 使用计数值对数组进行排序,iphone,ios,ios5,ios4,ios6,Iphone,Ios,Ios5,Ios4,Ios6,我有一个数组,其中包含一些字符串。为字符串的每个字符分配一个整数值。例如a=2,b=5,c=6,o=1,k=3等等 字符串中的最终值是字符值的总和。因此,对于示例字符串“BOOK”,该字符串将存储为“BOOK(7)”。类似地,每个字符串都有一个最终整数值。我想用存储在每个数组索引中的字符串中的这些最终整数值对这些数组进行排序。该数组包含超过200000个单词。因此,排序过程应该相当快。有什么办法吗 据我所知,您希望对包含以下格式的字符串的数组进行排序 a=3 您希望在忽略字符的情况下根据数字进
字符串中的最终值是字符值的总和。因此,对于示例字符串“BOOK”,该字符串将存储为“BOOK(7)”。类似地,每个字符串都有一个最终整数值。我想用存储在每个数组索引中的字符串中的这些最终整数值对这些数组进行排序。该数组包含超过200000个单词。因此,排序过程应该相当快。有什么办法吗 据我所知,您希望对包含以下格式的字符串的数组进行排序
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
据我所知,您希望对包含以下格式的字符串的数组进行排序
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];
}
将输出“书籍(10)”
您可以将ViewController更改为自己的类名 试试这种方法
+(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];
}
将输出“书籍(10)”
您可以将ViewController更改为自己的类名 一个简单的例子是,如果字符串结构始终相同,如“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);
NSMutableArray*arr=[NSMutableArray数组,其对象为:@“Book(99)”、@“Pencil(66)”、@“Trash(04)”、nil];
NSLog(@“%@”,arr);
NSMutableDictionary*dict=[NSMutableDictionary];
//找到数字并将每个元素存储在字典中
对于(int i=0;i一个简单的例子是,如果字符串结构始终相同,如“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);
NSMutableArray*arr=[NSMutableArray数组,其对象为:@“Book(99)”、@“Pencil(66)”、@“Trash(04)”、nil];
NSLog(@“%@”,arr);
NSMutableDictionary*dict=[NSMutableDictionary];
//找到数字并将每个元素存储在字典中
对于(int i=0;i首先-创建一个自定义对象以保存值。不要将值放入字符串中。
排序不是您的基本问题。问题是您正在将值保存到字符串中,而这些值很难从中提取
@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:
即可对数组进行排序。
注意,这将在性能上胜过所有其他答案,因为无需在每次比较时解析值。首先-创建自定义对象以保存值。不要将值放入字符串中。
排序不是您的基本问题。问题是您正在将值保存到字符串中,而这些值很难从中提取
@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:
即可对数组进行排序。
注意,这将在性能上胜过所有其他答案,因为不需要每次比较都解析值。我认为您应该使用正则表达式作为整数部分,然后您可以对数据进行排序。在这种情况下,我如何使用正则表达式?您的数组是这样的:[“book5”、“table3”、“pen2”]?据我所知,他的数组类似于[“BOOK(7)”、“Table(3)”、“Pen(2)”“].当你想以后使用它进行排序时,将总和放入字符串中是不太实际的。改变它,你的问题就会消失。我认为你应该使用正则表达式作为整数部分,然后你可以对数据进行排序。在这种情况下,我如何使用正则表达式?你的数组是这样的:[“book5”,“table3”,“pen2”]?据我所知,他的数组类似于[”BOOK(7)“,“表(3)”,“笔(2)”]。当你想稍后使用它进行排序时,将总和放入字符串中是不太实际的。更改它,你的问题就会消失。非常感谢Mat,非常感谢你的支持和时间,非常感谢Mat,非常感谢你的支持和时间,这真的让我发笑。你抽了什么烟?这真的让我很开心笑,你抽了什么?