如何在 iPhone 中对数字作为字符串的数组进行排序? [英] How to sort array having numbers as string in iPhone?

查看:32
本文介绍了如何在 iPhone 中对数字作为字符串的数组进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
对 NSArray 进行排序的最佳方法
使用特殊字符对数组进行排序 - iPhone

你好,
我有一个数组,其中我将数字存储为字符串.
如何按升序对这个数组进行排序?

Hello,
I have array in which i have numbers stored as string.
How to sort this array in acsending order?

推荐答案

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;
    }
}

这篇关于如何在 iPhone 中对数字作为字符串的数组进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆