我有一些名字的對象,就像button 1 , button 2 , button 11 ,button 111
,我想通過這個順序將這個對象添加到數組中:button 1 ,button 2 , button 11 , button 111
,但是我發現這些對象可以通過以下順序添加數組:button 1 , button 11,button 111 , button 2
.why?謝謝您的回答!如何通過這個順序將這些具有名稱的對象添加到數組中?
-1
A
回答
-1
如果你在Finder中有這個名字的文件,他們會以相同的方式排序。
得到你需要他們的名字按鈕001,按鈕002,按鈕011正確排序等
或者,你必須寫自己的排序描述符。
在這裏看到NSMutableArray sortUsingSelector:並在這裏爲您的排序比較NSSortDescriptor
1
1
您可以使用此代碼對數組進行排序。使用NSNumericSearch來搜索字符串中的數字值。
NSArray * ary = [[NSArray alloc] initWithObjects:@"button 1" , @"button 2" , @"button 11" ,@"button 111", nil];
ary = [ary sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [(NSString *)obj1 compare:(NSString *)obj2 options:NSNumericSearch];
}];
NSLog(@"ary : %@", ary);
和日誌顯示:
ary : (
"button 1",
"button 2",
"button 11",
"button 111"
)
1
我已經使用了NSNumericSearch | NSCaseInsensitiveSearch組合,以獲得最好的結果。我希望有人能夠將他們的一生攢下來。檢查結果。
NSArray * array =
[NSArray
arrayWithObjects:
[NSDictionary dictionaryWithObject: @"C3" forKey: @"sort"],
[NSDictionary dictionaryWithObject: @"C1" forKey: @"sort"],
[NSDictionary dictionaryWithObject: @"C100" forKey: @"sort"],
[NSDictionary dictionaryWithObject: @"Z0" forKey: @"sort"],
[NSDictionary dictionaryWithObject: @"Z1000" forKey: @"sort"],
[NSDictionary dictionaryWithObject: @"M" forKey: @"sort"],
[NSDictionary dictionaryWithObject: @"m" forKey: @"sort"],
[NSDictionary dictionaryWithObject: @"M" forKey: @"sort"],
[NSDictionary dictionaryWithObject: @"m" forKey: @"sort"],
[NSDictionary dictionaryWithObject: @"1A4" forKey: @"sort"],
[NSDictionary dictionaryWithObject: @"A2" forKey: @"sort"],
[NSDictionary dictionaryWithObject: @"A12" forKey: @"sort"],
[NSDictionary dictionaryWithObject: @"B4" forKey: @"sort"],
[NSDictionary dictionaryWithObject: @"B6" forKey: @"sort"],
[NSDictionary dictionaryWithObject: @"B100" forKey: @"sort"],
[NSDictionary dictionaryWithObject: @"B1" forKey: @"sort"],
[NSDictionary dictionaryWithObject: @"1a4" forKey: @"sort"],
nil];
NSSortDescriptor * descriptor =
[NSSortDescriptor
sortDescriptorWithKey: @"sort"
ascending: YES
comparator:
^NSComparisonResult(id obj1, id obj2)
{
return [obj1 compare: obj2 options: NSNumericSearch|NSCaseInsensitiveSearch];
}];
NSArray * sortedArray =
[array
sortedArrayUsingDescriptors:
[NSArray arrayWithObject: descriptor]];
NSLog(@"Original array:");
for(NSDictionary * value in array)
NSLog(@"%@", [value objectForKey: @"sort"]);
NSLog(@"Sorted array:");
for(NSDictionary * value in sortedArray)
NSLog(@"%@", [value objectForKey: @"sort"]);
結果
原始陣列:
C3
C1
C100
Z0
Z1000
M
m
M
m
1A4
A2
A12
B4
B6
B100
B1
1a4
排序後的數組:
1A4
1a4
A2
A12
B1
B4
B6
B100
C1
C3
C100
M
m
M
m
Z0
Z1000
相關問題
- 1. 如何通過名稱鍵合併這些數組對象?
- 2. 我應該如何將這個對象添加到數組...?
- 3. 如何通過PHP將JSON對象數組添加到MySQL中
- 4. 排序向量通過這些對象
- 5. 通過附加具有可更改值的通用名稱來標識對象名稱。這可能嗎?
- 6. 如果對象名稱來自另一個數組,則如何將對象添加到數組中?
- 7. 如何通過迭代將這些放入對象中? JS
- 8. 如何添加這些數組?
- 9. 對象 - 對象中的對象 - 這些對象的數組
- 10. 如何根據數組中的這些鍵的順序,通過鍵對ruby散列進行排序?
- 11. 如何將對象添加到數組
- 12. 如何通過對象數組循環,並添加參考名稱
- 13. 將兩個對象合併到一個對象中。我有這個數組
- 14. PhP - 如何獲得這些對象的名稱
- 15. PHP - 將具有其屬性的對象添加到數組
- 16. 如何在javascript中對這個對象數組進行排序?
- 17. Flex動態對象名稱,並將對象添加到對象?
- 18. 如何將這些函數合併到具有屬性的單個函數中?
- 19. 將javascript對象轉換爲具有特定順序的數組
- 20. 如何通過這個數組在PHP
- 21. 如何通過輸入表單將對象數組添加到html列表中?
- 22. 將對象添加到數組數組
- 23. C#json序列化 - 如何將名稱添加到列表中的對象?
- 24. 通過循環將字符串對象添加到數組
- 25. 如何將對象從一個數組添加到場景中?
- 26. 如何改變這些div的順序?
- 27. 如何將這些日期對象合併到一個對象中
- 28. 如何通過這些對象的屬性的排序LEN對象的名單? - Python的
- 29. 如何按這些對象屬性排列對象數組的數組
- 30. 在android中JSON數組中具有相同對象名稱的多個數組?
實際上,搜索器總是用適當的數字在Mac OS X中排序。它是CF和Cocoa排序選項之一。只有在Mac OS中才會按ASCII順序排序。 – 2009-04-14 03:27:59