首页 \ 问答 \ 按字母排序NSArray对象然后编号[重复](Sorting an NSArray of Objects by Letter then number [duplicate])

按字母排序NSArray对象然后编号[重复](Sorting an NSArray of Objects by Letter then number [duplicate])

这个问题在这里已有答案:

我有一个“房间”对象数组,每个对象都有一个名为“room.descritpion”的属性,它是一个包含字母数字字符组合的NSString。 这些房间需要使用“room.description”属性按字母顺序升序排序,然后按数字顺序升序排序。 我可以很容易地按字母顺序排序,但我正在努力如何合并第二种类型,这将基于字母后面的数字,例如L21 L19 A34 A12应该在阵列中订购为A12,A34,L19,L21等等上。 搜索的优先级应该是字母,然后是数字,所以只有数字的房间出现在数组的末尾。 字母和数字并不总是以相同的顺序,即字母并不总是第一个。 我已经彻底阅读了文档,但似乎无法找到解决方法。 我很乐意使用块,如果它使过程更容易。 任何帮助将不胜感激。


This question already has an answer here:

I have an array of "room" objects, each of these has a property called "room.descritpion" that is an NSString containing a mix of alphanumerical characters. These rooms need to be sorted in an array using the "room.description" property with ascending alphabetical order and then by number order ascending. I can get the alphabetical sorting done easily but I am struggling with how to incorporate the second sort which will be based on the number following the letter e.g. L21 L19 A34 A12 should be ordered in the array as A12, A34, L19, L21 and so on. The priority of the search should be letter first and then by number so a room with only numbers comes at the end of the array. The letters and numbers are not always in the same order i.e. the letter is not always first. I have read the documentation thoroughly but cannot seem to find the way to approach this. I am happy to use blocks if it makes the process easier. Any help would be greatly appreciated.


原文:https://stackoverflow.com/questions/34546050
更新时间:2020-10-17 14:10

最满意答案

这个

var arr = ["A6", "A2", "3", "B4", "L8", "4", "B7"]
let sortedArr = arr.sort({String($0) < String($1)})
print(sortedArr)

将打印出来

["3", "4", "A2", "A6", "B4", "B7", "L8"]

This

var arr = ["A6", "A2", "3", "B4", "L8", "4", "B7"]
let sortedArr = arr.sort({String($0) < String($1)})
print(sortedArr)

Will print out

["3", "4", "A2", "A6", "B4", "B7", "L8"]
2015-12-31

相关问答

更多

在NSArray中获取重复项(Get duplicates in NSArray)

使用NSCountedSet并仅打印为countForObject:方法返回数字> 1的元素 Use an NSCountedSet and only print the elements that returns a number>1 for countForObject: method

洗牌NSArray(Shuffle NSArray)

如果您只需要选择一个随机对象,则可以使用以下内容。 它比实际改组阵列更有效。 uint32_t rnd = arc4random_uniform([array count]); MyObject *randomObject = [array objectAtIndex:rnd]; 相信亚当的 答案 。 I managed to "solve" my issue by doing the following: It maybe messy but I would like some input ...

NSArray中NSString对象的参数(parameter for NSString objects in NSArray)

希望我理解正确: - (NSString*)hasCrazy:(NSArray*)stringArray index:(int)i { //Check that we don't go out of bounds if (stringArray.count < 2) return @" "; if ([stringArray[i-2] isEqualToString:@"crazy"]) return @"crazy"; ...

排序NSArray(Sorting a NSArray)

我假设你正在使用NSMutableArray,因为NSArray是不可变的。 当你对某物进行排序时,你希望将最终结果排列为 x1 <= x2 <= x3 <= x4 <= ... <= xN 如何定义这个<= ? ObjC中有3个解决方案。 1. -sortUsingSelector: [arr sortUsingSelector:@selector(foo:)]意味着它的元素将被比较为* x <= y is equivalent to [x foo:y] <= 0 例如, ...

增加NSArray中重复的NSDictionary对象的数量?(Increment count of duplicate NSDictionary objects in an NSArray?)

我会创建一个包含两个项目的新dict:库存字典和数量。 我会像itemsArray一样添加一个项目(未经测试,所以要小心拼写错误): BOOL found = NO; for (NSDictionary *dict in itemsArray) { if ([[dict objectForKey: @"inventorydict"] isEqual: inventory.itemDict]) { [dict setObject: [NSNumber numberWi ...

RACSignal用于NSArray对象(RACSignal for an NSArray of objects)

要观察所有最新的视图模型(以及最新的视图模型),我们需要在每次更改阵列时设置新的KVO观测值。 表示这一点的最自然的方式是信号信号。 每个“内部”信号代表一个版本的viewModels上的一组观察,然后我们将使用-switchToLatest来确保只有最新的信号生效: @weakify(self); RACSignal *enabled = [[RACObserve(self, viewModels) // Map _each_ array of view models to a sig ...

对包含NSDate对象的NSArray进行排序(Sorting NSArray containing NSDate objects)

在Swift中使用本机Array类型。 如果您与ObjC的旧代码连接,请使用: let array = unsortedArray.sortedArrayUsingSelector("compare:") Using the native Array type in Swift. If you are interfacing with legacy code from ObjC, use this: let array = unsortedArray.sortedArrayUsingSelect ...

NSArray按功能排序(NSArray sorted by function)

您可以定义自定义比较器以使用您要查找的那种复合排序键。 如果没有充分的理由在字典中使用该排序键,除了执行排序之外,请不要将项添加到字典中。 array = [array sortedArrayUsingComparator: ^(id obj1, id obj2) { int rating1 = [[obj1 objectForKey:@"rating"] intValue]; int numberOfVotes1 = [[obj1 objectForKey:@"numberOfV ...

按字母排序NSArray对象然后编号[重复](Sorting an NSArray of Objects by Letter then number [duplicate])

这个 var arr = ["A6", "A2", "3", "B4", "L8", "4", "B7"] let sortedArr = arr.sort({String($0) < String($1)}) print(sortedArr) 将打印出来 ["3", "4", "A2", "A6", "B4", "B7", "L8"] This var arr = ["A6", "A2", "3", "B4", "L8", "4", "B7"] let sortedArr = arr.sor ...

如何从NSArray中删除重复的对象?(How to remove duplicated objects from NSArray?)

如果你的名字是字符串,你可以从数组创建NSSet ,它只有不同的名称。 let names = ["John", "Marry", "Bill", "John"] println(names) let set = NSSet(array: names) println(set.allObjects) 打印: "[John, Marry, Bill, John]" "[Bill, John, Marry]" 更新#1 对于有问题的新信息(代码片段),它可能看起来像这样 var set = Se ...

相关文章

更多

最新问答

更多
  • Haml + ActionMailer - Rails?(Haml + ActionMailer - Rails?)
  • 将数据从div标签传递到javascript函数[关闭](Pass data from div tags to javascript function [closed])
  • 如何使用imaplib创建电子邮件并将其发送到特定邮箱(How to create an email and send it to specific mailbox with imaplib)
  • Ruby - 使用`require`命令(Ruby - working with `require` command)
  • 用ng值检索时,在md-input-container中隐藏输入文本字段的值(label hiding input text field value in md-input-container when retrieving with ng-value)
  • 以编程方式将字符串宽度值插入sprintf()(Programmatically insert string width value into sprintf())
  • Matplotlib计算给定字符串的轴坐标范围(Matplotlib Calculate Axis Coordinate Extents Given String)
  • 在同一个流连接上返回多个结果以实现HTML5 Server Sent Events(Returning multiple results on the same stream connection to implement HTML5 Server Sent Events)
  • 限制对特定URL的访问(Apache Tomcat)(Restrict access to specific URL (Apache Tomcat))
  • R中的时间序列与ggplot2(Time Series in R with ggplot2)
  • 自动URL参数编码失败(Automatic URL Parameter Encoding Failing)
  • 是否有任何关于JRuby + Clojure集成的开源示例?(Are there any good open source examples of JRuby + Clojure integration?)
  • ActiveX被认为是旧的/过时的标准吗?(Is ActiveX considered old/outdated by todays standards?)
  • SQL Server中的XML查询(XML Query within SQL Server)
  • PL / SQL:在更新尝试时,删除导致unique_violation异常的行(PL/SQL: on update attempt, delete row which causes unique_violation exception)
  • webpack css-loader localIdent名称哈希长度(webpack css-loader localIdent name hash length)
  • 基诺数解析器(Keno number parser)
  • R:索引数据框列的名称范围(R: Index data frame columns by ranges of their names)
  • 限制ASP .net和Javascript中的多个事件(Restrict multiple events in ASP .net & Javascript)
  • 更改行时SQL查询不更新表(SQL query not updating table when row is changed)
  • JavaScript,document.getElementById不从窗体中抓取?(JavaScript, document.getElementById not grabbing from form?)
  • h2数据库上的Mybatis无法插入数据(Mybatis on h2 database can't insert data)
  • KeyboardWillHideNotification未在iOS 9中触发(KeyboardWillHideNotification not firing in iOS 9)
  • jQuery UI Droppable - 如何实际更改HTML?(jQuery UI Droppable - How to actually change the HTML?)
  • 无法将APK安装到root设备中(Can't install APK into rooted device)
  • 在滚动浏览器时,使jQuery UI对话框不改变它的位置(相对于浏览器)(make jQuery UI dialog not change it's position (relative to the browser) when scrolling browser)
  • 我们可以在NativeScript中使用nedb吗?(Can we use nedb with NativeScript?)
  • 使用CancellationToken的竞争条件,其中CancellationTokenSource仅在主线程上被取消(Race condition with CancellationToken where CancellationTokenSource is only cancelled on the main thread)
  • 使用Framework7从JS更新HTML视图中的数据(Update data in HTML view from JS using Framework7)
  • sql server:选择一个外键为NULL的字段(sql server: select a NULL field that is foreign key)