Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 是否有任何方法可以反转NSNumericSearch,以便将包含数字的NSString按9、8、7、6顺序排列。。。等_Iphone_Objective C_Xcode_Cocoa_Ipad - Fatal编程技术网

Iphone 是否有任何方法可以反转NSNumericSearch,以便将包含数字的NSString按9、8、7、6顺序排列。。。等

Iphone 是否有任何方法可以反转NSNumericSearch,以便将包含数字的NSString按9、8、7、6顺序排列。。。等,iphone,objective-c,xcode,cocoa,ipad,Iphone,Objective C,Xcode,Cocoa,Ipad,我有返回NSComparisonResult的代码。当数组是仅包含字母字符的字符串时,我的代码非常适合按升序和降序对数组进行排序 当NSSTRING中包含数字时,例如Seinfeld(第5季)和Seinfeld(第6季)它们按照5和6的顺序正确地升序排序。但降序后仍将其分为5和6。下面是一个例子: Ascending: Batman, Seinfeld (Season 5), Seinfeld (Season 6), Zoolander Descending: Zoolander, Seinf

我有返回NSComparisonResult的代码。当数组是仅包含字母字符的字符串时,我的代码非常适合按升序和降序对数组进行排序

当NSSTRING中包含数字时,例如
Seinfeld(第5季)
Seinfeld(第6季)
它们按照5和6的顺序正确地升序排序。但降序后仍将其分为5和6。下面是一个例子:

Ascending:
Batman,
Seinfeld (Season 5),
Seinfeld (Season 6),
Zoolander

Descending:
Zoolander,
Seinfeld (Season 5),
Seinfeld (Season 6),
Batman
我使用的降序代码如下所示以返回比较:

return [(NSString *)secondString compare:(NSString *)firstString options:NSNumericSearch];
有没有关于如何反转包含数字的字符串的想法


顺便说一下,我不能在我的情况下使用NSSortDescriptor。我的代码需要构建一个自定义函数,因为我必须在排序之前进行一些字符串追加等操作。为了简单起见,我没有把它全部包括进去。

只需返回-(NSComparisonResult)

所以我知道这可能会很痛苦,但我相信从长远来看,这对你会有用。我建议您编写自己的排序函数,下面是我将要做的。您可能需要两个函数,但通过这种方式,您可以调整它们,使其完全符合您的需要。
为了对它们进行排序,我建议查看每个字符的ascii值并将它们加在一起,然后将它们与另一个ascii值进行比较,如果降序返回较低的值,如果升序返回较高的值。至于排序算法部分,您可以使用气泡排序。这是最简单的,但它是缓慢的,所以如果这是一些需要快速的东西,那么我建议花时间看看合并排序。无论如何,我希望这能有所帮助。如果您需要帮助纠正排序方法,我很乐意为您提供帮助,但您似乎是一名非常优秀的编码员:)

此代码适合我:

 NSArray *array = [NSArray arrayWithObjects:@"Batman",
              @"Seinfeld (Season 5)",
              @"Seinfeld (Season 6)",
              @"Zoolander", nil];
//为了提升

NSArray *sortedArray = [array sortedArrayUsingComparator: ^(id firstString, id secondString) {
  return [(NSString *)firstString compare:(NSString *)secondString  options:NSNumericSearch];
}];

  NSLog(@"========= %@================= ", sortedArray);
//下降

NSArray *sortedArray = [array sortedArrayUsingComparator: ^(id firstString, id secondString) {
  return [(NSString *)secondString compare:(NSString *)firstString  options:NSNumericSearch];
}];

本杰明·梅奥的回答:

假设您的“升序”比较函数是:

static NSComparisonResult compareAscending(id s1, id s2, void *context) {
    return [s1 compare:s2 options:NSNumericSearch];
}
那么这应该是您的“降序”比较函数:

static NSComparisonResult compareDescending(id s1, id s2, void *context) {
    return -compareAscending(s1, s2, context);
}

基本上与上面的评论相同,但这里有完整的源代码和输出来证明它是有效的(iOS 4.3模拟器测试):

输出:

2011-10-19 02:09:07.557 test2222[16793:207] starting: (
  "Seinfeld (Season 6)",
  Batman,
  "Seinfeld (Season 5)",
  Zoolander
)

2011-10-19 02:09:07.558 test2222[16793:207] sort1: (
  Batman,
  "Seinfeld (Season 5)",
  "Seinfeld (Season 6)",
  Zoolander
)

2011-10-19 02:09:07.558 test2222[16793:207] sort2: (
  Zoolander,
  "Seinfeld (Season 6)",
  "Seinfeld (Season 5)",
  Batman
) 

在回答Ethan Allen的问题时,以下是前面有数字的测试的输出:

2011-12-14 21:01:26.535 Test[7475:f803] starting: (
    "2001 Seinfeld (Season 6)",
    "1999 Batman",
    "2112 Seinfeld (Season 5)",
    "10 Zoolander"
)
2011-12-14 21:01:26.537 Test[7475:f803] sort1: (
    "10 Zoolander",
    "1999 Batman",
    "2001 Seinfeld (Season 6)",
    "2112 Seinfeld (Season 5)"
)
2011-12-14 21:01:26.537 Test[7475:f803] sort2: (
    "2112 Seinfeld (Season 5)",
    "2001 Seinfeld (Season 6)",
    "1999 Batman",
    "10 Zoolander"
)

返回当前比较的负数:alphanum算法是我最喜欢的智能排序算法,但没有一个objective c版本(cpp)@JacobJennings这正是Cocoa的
NSNumericSearch
选项所做的:我不明白。。。您的升序和降序代码完全相同。嘿,Ethan,在降序方法中尝试使用以下代码:return[(NSString*)secondString compare:(NSString*)firstString选项:NSNumericSearch];现在,前面有编号的项目是什么?如《12只猴子》、《2001太空漫游》、《300》、《3D电影》、《1900年是伟大的一年》等。这些会按数字顺序正确排序吗?@Ethanalen-非常容易测试,因为我将所有代码直接粘贴在上面:)新项目,将其粘贴到应用程序代理应用程序中。应用程序完成启动方法后,您就会知道答案。编写测试程序是开发人员的一项关键技能。我花了不到2分钟的时间复制上面的代码,创建一个新的空iOS应用程序,将代码粘贴到app delegate doe finish启动方法中,修改字符串以将数字放在前面并运行它。答案补充了上述内容。
2011-12-14 21:01:26.535 Test[7475:f803] starting: (
    "2001 Seinfeld (Season 6)",
    "1999 Batman",
    "2112 Seinfeld (Season 5)",
    "10 Zoolander"
)
2011-12-14 21:01:26.537 Test[7475:f803] sort1: (
    "10 Zoolander",
    "1999 Batman",
    "2001 Seinfeld (Season 6)",
    "2112 Seinfeld (Season 5)"
)
2011-12-14 21:01:26.537 Test[7475:f803] sort2: (
    "2112 Seinfeld (Season 5)",
    "2001 Seinfeld (Season 6)",
    "1999 Batman",
    "10 Zoolander"
)