2012-11-03 35 views
1

我有一個包含一些字符串的數組。對於字符串的每個字符,都會分配一個整數值。例如a = 2,b = 5,c = 6,o = 1,k = 3等用計數值對數組排序

字符串中的最終值是字符值的總和。因此,對於字符串「BOOK」的例子,字符串將被存儲爲「BOOK(7)」。同樣,每個字符串都會有一個最終的整數值。我想用存儲在每個數組索引中存在的字符串中的這些最終整數值排序這些數組。該數組包含超過200,000個字。所以排序過程應該非常快。有沒有辦法呢?

+1

我認爲你應該使用整數部分的正則表達式,然後你可以排序數據 –

+0

我怎麼能在這種情況下使用正則表達式? – Kiron

+0

你的數組是這樣的:[「book5」,「table3」,「pen2」]? – sunkehappy

回答

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)」

+0

感謝Mat非常感謝您的支持和時間 – Kiron

+0

不客氣;) – Mat

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 
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)」

你自己的類名您可以更改視圖控制器;

+1

這真的讓我發笑。你吸了什麼? – Till

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:。 請注意,這將在性能上超過所有其他答案,因爲不需要每次比較都解析該值。