2014-02-15 34 views
2

我有一個NSArrayNSDictionaries,在NSDictionary我有一個名爲'rowID'的元素保存爲NSString,但保存在其中的所有內容都是一個數字..但仍然是一個字符串值。使用值排序NSArictionary的NSArray

我想知道如何根據此值對數組進行排序,但在排序方面1-100,當前我排序時它先排在第一位......當它應該是1-10時。

這就是我如何排序。

NSArray *tempSortedItemsArray = [itemArray sortedArrayUsingDescriptors: 
            @[[NSSortDescriptor 
             sortDescriptorWithKey:@"rowID" ascending:YES]]]; 

任何幫助,將不勝感激。

回答

6

試試這個: -

NSArray *aSortedArray = [itemArray sortedArrayUsingComparator:^(NSMutableDictionary *obj1,NSMutableDictionary *obj2) { 
    NSString *num1 =[obj1 objectForKey:@"rowID"]; 
    NSString *num2 =[obj2 objectForKey:@"rowID"]; 
    return (NSComparisonResult) [num1 compare:num2 options:(NSNumericSearch)]; 
}]; 
+0

這是不正確。 OP聲明rowID是'NSString'對象,而不是'NSNumber'對象。這將最終做相同的不正確的字符串比較。 – rmaddy

+0

它工作正常我的最後測試它,如果你發現不正確的結果,那麼我會刪除我的答案 – Leena

+0

這是正確的,如果'rowID'值實際上是'NSNumber'對象,但OP有'NSString'對象。 – rmaddy

1

如果使用initWithKey:ascending:selector:sortDescriptorWithKey:ascending:comparator:創建排序描述符,則可以指定用於比較對象的選擇器。

現在問題是哪個選擇器可以通過。那麼你可以在NSString上創建一個Category,實現一個自定義排序函數,該函數根據字符串的數字值進行排序。從文件中記住:

選擇器必須指定一個由keyPath標識的 屬性的值實現的方法。用於比較 的選擇器傳遞一個參數,該對象與self進行比較,並且 必須返回相應的NSComparisonResult常量。該選擇 必須有相同的方法簽名爲:

- (NSComparisonResult)localizedCompare:(NSString *)aString 

在指定的屬性鍵的對象,相對於 收集每一個對象,必須實現比較選擇用於創建 排序描述。如果未指定自定義選擇器,則對象必須執行 比較:。

1

嘗試這樣的: -

NSDictionary *row1 = [NSDictionary dictionaryWithObjectsAndKeys:@"1",@"rowId",nil]; 
NSDictionary *row2 = [NSDictionary dictionaryWithObjectsAndKeys:@"2",@"rowId",nil]; 
NSDictionary *row3 = [NSDictionary dictionaryWithObjectsAndKeys:@"3",@"rowId",nil]; 

NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:row1,row2,row3,nil]; 

NSSortDescriptor *desc = [[NSSortDescriptor alloc] initWithKey:@"rowId" ascending:YES]; 
[arr sortUsingDescriptors:[NSArray arrayWithObject:desc]]; 

// before sort 
NSLog(@"Before %@",arr); 
[arr sortedArrayUsingComparator:^(NSDictionary *item1, NSDictionary *item2) { 
    NSString *first = [item1 objectForKey:@"rowId"]; 
    NSString *second = [item2 objectForKey:@"rowId"]; 
    return [first compare:second options:NSNumericSearch]; 
}]; 
// After sort 
NSLog(@"After %@",arr); 
+1

這是什麼用戶已經在做什麼不同? – Merlevede

+0

更新仍然不正確。這會進行字母排序而不是所需的數字排序。 – rmaddy

2

您可以通過下面這段代碼獲取數字排序: -

NSMutableArray *tmpAr = [[NSMutableArray alloc] init]; 

NSMutableDictionary *tmpDict = [[NSMutableDictionary alloc] init]; 
[tmpDict setObject:@"name" forKey:@"number5"]; 
[tmpDict setObject:@"5" forKey:@"id"]; 
[tmpAr addObject:tmpDict]; 

tmpDict = [[NSMutableDictionary alloc] init]; 
[tmpDict setObject:@"name" forKey:@"number3"]; 
[tmpDict setObject:@"3" forKey:@"id"]; 
[tmpAr addObject:tmpDict]; 

tmpDict = [[NSMutableDictionary alloc] init]; 
[tmpDict setObject:@"name" forKey:@"number2"]; 
[tmpDict setObject:@"2" forKey:@"id"]; 
[tmpAr addObject:tmpDict]; 

tmpDict = [[NSMutableDictionary alloc] init]; 
[tmpDict setObject:@"name" forKey:@"number1"]; 
[tmpDict setObject:@"1" forKey:@"id"]; 
[tmpAr addObject:tmpDict]; 

tmpDict = [[NSMutableDictionary alloc] init]; 
[tmpDict setObject:@"name" forKey:@"number4"]; 
[tmpDict setObject:@"4" forKey:@"id"]; 
[tmpAr addObject:tmpDict]; 

tmpDict = [[NSMutableDictionary alloc] init]; 
[tmpDict setObject:@"name" forKey:@"number25"]; 
[tmpDict setObject:@"25" forKey:@"id"]; 
[tmpAr addObject:tmpDict]; 

tmpDict = [[NSMutableDictionary alloc] init]; 
[tmpDict setObject:@"name" forKey:@"number10"]; 
[tmpDict setObject:@"10" forKey:@"id"]; 
[tmpAr addObject:tmpDict]; 

tmpDict = [[NSMutableDictionary alloc] init]; 
[tmpDict setObject:@"name" forKey:@"number7"]; 
[tmpDict setObject:@"7" forKey:@"id"]; 
[tmpAr addObject:tmpDict]; 

NSLog(@"tmpar1 = %@",tmpAr); 

[tmpAr sortUsingComparator: 
^(id obj1, id obj2) 
{ 
    NSInteger value1 = [[obj1 objectForKey: @"id"] intValue]; 
    NSInteger value2 = [[obj2 objectForKey: @"id"] intValue]; 
    if (value1 > value2) 
    { 
     return (NSComparisonResult)NSOrderedDescending; 
    } 

    if (value1 < value2) 
    { 
     return (NSComparisonResult)NSOrderedAscending; 
    } 
    return (NSComparisonResult)NSOrderedSame; 
}]; 

NSLog(@"tmpar2 = %@",tmpAr);