如何在数组中将数字排序为字符串?

问题描述:

可能重复:
best way to sort NSArray
Sort an array with special characters - iPhone如何在数组中将数字排序为字符串?

你好,
我有数组中,我有存储字符串的数字。
如何按acsending命令排序该数组?

+0

已经在此论坛讨论 Rams 2011-05-23 12:27:39

NSArray *sortedArray = nil; 
sortedArray = [oldArray sortedArrayUsingFunction:sortArray context:NULL]; 

NSInteger intSort(id num1, id num2, void *context) { 

    // OR: float n1 = [num1 floatValue]; etc. 

    int n1 = [num1 intValue]; 
    int n2 = [num2 intValue]; 
    if (n1 < n2) { 
     return NSOrderedAscending; 
    } else if (n1 > n2) { 
     return NSOrderedDescending; 
    } else { 
     return NSOrderedSame; 
    } 
}