数组排序与字符串包含数字 [英] Sorting an Array with Strings that contains Numbers

查看:161
本文介绍了数组排序与字符串包含数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/9674707/sorting-nsstring-values-as-if-nsinteger-using-nssortdescriptor\">Sorting NSString的值,如果使用NSInteger的 NSSortDescriptor

Possible Duplicate:
Sorting NSString values as if NSInteger using NSSortDescriptor

我有一个数组,我充满了我的NSMutableDictionary ..和我用这样的:

I have an Array that I fill with my NSMutableDictionary.. and I use this:

myArray =[[myDict allKeys]sortedArrayUsingSelector:@selector(IDONTKNOW:)];

myDicts的AllKeys是NSString的......像123.423或423.343 ...我需要排序增量数字新myArray的.. 12.234 45.3343 522.533 5432.66等等等等

AllKeys of myDicts are NSStrings... like 123.423 or 423.343... I need to sort the new myArray by incremental numbers.. 12.234 45.3343 522.533 5432.66 etc etc

什么必须@selector插入正确做到这一点?谢谢

What must insert in @selector to do this properly? Thanks

推荐答案

您可以使用<一个href=\"https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSSortDescriptor_Class/Reference/Reference.html\"><$c$c>NSSortDescriptor并通过的doubleValue 的关键。

//sfloats would be your [myDict allKeys]
NSArray *sfloats = @[ @"192.5235", @"235.4362", @"3.235", @"500.235", @"219.72" ];
NSArray *myArray = [sfloats sortedArrayUsingDescriptors:
                    @[[NSSortDescriptor sortDescriptorWithKey:@"doubleValue" 
                                                    ascending:YES]]];

NSLog(@"Sorted: %@", myArray);

这篇关于数组排序与字符串包含数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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