在對數組進行排序時,我得到:1,10,2,3,4,5,6,7,8,9。什麼地方出了錯 ?分類列表數組問題
我的代碼是:
NSArray *sortedArray = [optionKeys sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
其中optionKeys是:7,3,8,4,9,5,1,6,2,10
我還曾試圖CaseInsensitiveCompare:並獲得相同的結果。
在對數組進行排序時,我得到:1,10,2,3,4,5,6,7,8,9。什麼地方出了錯 ?分類列表數組問題
我的代碼是:
NSArray *sortedArray = [optionKeys sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
其中optionKeys是:7,3,8,4,9,5,1,6,2,10
我還曾試圖CaseInsensitiveCompare:並獲得相同的結果。
字符串比較將在10之後放置10。您需要使用數字比較函數。這可能有所幫助:How to let the sortedarrayusingselector using integer to sort instead of string
您正在執行字符串排序而不是數字排序。見this answer。
看看這個answer。你想要做一個數字排序而不是一個字符串排序。
可能的重複[如何讓sortedArrayUsingSelector使用整數來排序而不是String?](http://stackoverflow.com/questions/2752992/how-to-let-the-sortedarrayusingselector-using-integer-to-排序INSTEAD-OF-字符串) – 2011-06-01 16:01:43