简体   繁体   English

我在NSString上有一个NSArray,但有些字符串只是数字。 我如何正确排序数字?

[英]I have an NSArray on NSString's, but some of the strings are only numbers. How do I sort numerically properly?

My NSArray contains 100, 110, 91, 98, and 87, all NSStrings. 我的NSArray包含100,110,91,98和87,所有NSStrings。

I'd like to sort them to show up in this order: 87, 91, 98, 100, 110. 我想按顺序排列它们:87,91,98,100,110。

Instead they are showing up as 100, 110, 87, 91, 98. 相反,它们显示为100,110,87,91,98。

I can't start the array with all ints instead of strings unfortunately. 不幸的是,我不能用所有的int而不是字符串来启动数组。 Anyone have any ideas on how to tell if the array contains ONLY numbers from strings, maybe using a for loop? 任何人都有任何想法如何判断数组是否只包含字符串中的数字,也许使用for循环? Then I could just convert them all to ints within the array and do the sort that way. 然后我可以将它们全部转换为数组中的整数并按此方式进行排序。 Or maybe there is an easy way I am missing? 或者也许有一种我想念的简单方法?

Let me know if anyone has any ideas. 如果有人有任何想法,请告诉我。

You can use the NSNumericSearch option to the NSString compare function to sort strings numerically. 您可以使用NSNumericSearch选项来NSString比较函数以数字方式对字符串进行排序。 To do this with an NSArray, you will need to write a block or function to call the compare:options: method. 要使用NSArray执行此操作,您需要编写一个块或函数来调用compare:options:方法。

NSArray *theStrings; // Contains strings, some of which are numbers
NSArray *theSortedStrings = [theStrings sortedArrayUsingComparator:^(id obj1, id obj2) {
    return [(NSString *)obj1 compare:(NSString *)obj2 options:NSNumericSearch];
}];

This will also sort numerically if there is a numer within a string, ie "abcd89" will come before "abcd123" . 如果字符串中有一个数字,也将在数字上排序,即"abcd89"将出现在"abcd123"之前。

There are a whole mess of options for sorting an array ; 排序数组有很多选项 ; that isn't really the problem. 这不是真正的问题。

The sort methods always put the objects in order. 排序方法总是按顺序放置对象。 The order is up to the objects, or to you. 订单取决于对象或您。

So what you need to do is one of the following: 所以你需要做的是以下之一:

  1. Get the strings to compare themselves numerically. 获取字符串以数字方式比较自己。
  2. Get the array to ask you to compare them, then compare them numerically. 获取数组要求您比较它们,然后用数字比较它们。

Door number one is a trick and a half. 第一门是一招半。 NSString and the other class clusters don't let you inherit implementations; NSString和其他类集群不允许您继承实现; you can make your own string class, but you'd basically need to do everything from scratch (or wrap a string within a string and forward every message except compare: to it). 你可以创建自己的字符串类,但是你基本上需要从头开始做任何事情(或者在字符串中包装一个字符串并将除了compare:之外的所有消息转发给它)。

Door number two is much easier. 门2号更容易。 Nearly every one of the sort methods takes either a block or a function, which you implement; 几乎每一个排序方法都采用你实现的块或函数; either way, the block/function takes two of the objects from the array, compares them, and returns the order they're in. 无论哪种方式,块/函数从数组中获取两个对象,比较它们,并返回它们所在的顺序。

Your block/function should send a compare:options: message to one of the two strings it receives, passing the NSNumericSearch option , and return the result. 您的块/函数应将compare:options:消息发送到它接收的两个字符串之一,传递NSNumericSearch选项 ,并返回结果。

With NSArray asking you how the strings compare and you asking the strings how they compare numerically, the array will then sort its objects in numeric order. 使用NSArray询问字符串如何比较,并询问字符串如何以数字方式进行比较,然后数组将按数字顺序对其对象进行排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM