iOS 比较 NSArray 中 NSString 的 intValue
我有一个 dico NSDictionary 包含以下内容: { 123 = , 125 = , ... 73 = 123
, 125, .., 73 是 NSString。
我想要一个包含按 id 排序的对象的 NSArray:
[ <MyObject:73>, <MyObject:123>, ..., <MyObject:125> ]
最有效的方法是什么?
我使用 [[dico allKeys]sortedArrayUsingSelector:@selector(compare:)]
来键入有序键列表,但由于键是 NSString,它们的排序方式不正确(我最终可能会得到 [ 42、46、5、56....]。
I have a dico NSDictionary containing things like:
{
123 = ,
125 = ,
...
73 =
}
123, 125, .., 73 are NSString.
I'd like to have an NSArray containing the objects ordered by their id:
[ <MyObject:73>, <MyObject:123>, ..., <MyObject:125> ]
What is the most efficient way to do so ?
I used [[dico allKeys] sortedArrayUsingSelector:@selector(compare:)]
to key a list of ordered keys but as the keys are NSString they are not ordered the right way (I could end up with [ 42, 46, 5, 56. ...].
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当对代表数字的 NSString 进行排序时,您希望使用 NSNumericSearch 选项将它们像数字一样排序,例如 1 < > 20< 100< 1000。这是一个例子:
When sorting NSStrings that represent numbers you want to use the NSNumericSearch option to sort them like numbers, e.g. 1 < 20 < 100 < 1000. Here's an example:
您可以使用 NSDictionary 的 keysSortedWithCompartor 方法,在其中您可以提供一个比较器来以您希望的方式比较键(在您的情况下,通过将它们作为整数进行比较)。
You could use the keysSortedWithCompartor method of your NSDictionary, where you can supply a comparator to compare the keys whatever way you wish (in your case, by comparing them as integers).