数组无法正确排序:Swift 4

编程入门 行业动态 更新时间:2024-10-28 02:26:03
本文介绍了数组无法正确排序:Swift 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图对一组自定义对象进行排序,但是由于某种原因,几周后起作用的代码不再起作用.它应该检查$ 0和$ 1是否具有相同的日期,并且如果它们具有相同的日期,则应该按id对数组进行排序,但是当前它无法正确排序,并且在我打印数组时,我得到以下输出:

I was trying to sort an array of custom objects but for some reason, the code that worked a few weeks back won't work anymore. It is supposed to check if $0 and $1 have the same date, and if they do, it is supposed to sort the array by id, but currently it won't sort correctly and when I print the array, I get the following output:

11-07-2017 : 1 11-07-2017 : 10 11-07-2017 : 11 11-07-2017 : 12 11-07-2017 : 13 11-07-2017 : 14 11-07-2017 : 15 11-07-2017 : 16 11-07-2017 : 17 11-07-2017 : 18 11-07-2017 : 19 11-07-2017 : 2 11-07-2017 : 20 11-07-2017 : 3 11-07-2017 : 4 11-07-2017 : 5 11-07-2017 : 7 11-07-2017 : 8 11-07-2017 : 9 11-08-2017 : 1 11-08-2017 : 2 11-09-2017 : 1 11-09-2017 : 2

从上面可以看出,只有几个条目的日期可以正确排序,但是有多个条目的日期(11-07-17)不能正确排序.

As can be seen above, the dates that only have a few entries sort correctly, but the dates with more entries (11-07-17) don't.

下面是我的代码:

阵列模型:

struct BalanceUser { var id = "" var name = "" var date = "" }

当前排序代码:

self.sortedTableArray.sort(by: { if $0.date != $1.date { return $0.date < $1.date } else { return $0.id < $1.id } })

Firebase代码(按要求):

Firebase Code (As Requested):

ref.child("Admin").child("Balances").observeSingleEvent(of: .value, with: { (snapshot) in let value = snapshot.value as? NSDictionary if value!.count > 1 { let specificValues = value?.allKeys for balanceUser in specificValues! { var user = BalanceUser() user.date = balanceUser as! String if balanceUser as? String != "balance" { var i = 0 var counter = 0 while i < 101 { self.ref.child("Admin") .child("Balances") .child(balanceUser as! String) .child(String(i)) .observeSingleEvent(of: .value, with: { (snapshot) in let nameValue = snapshot.value as? NSDictionary if nameValue != nil { user.id = counter var j = 0 while j < (nameValue?.count)! { let item = nameValue?.allKeys[j] as? String var aItem = "" if let item = nameValue?.allValues[j] as? String { aItem = item } else if let item = nameValue?.allValues[j] as? NSNumber { aItem = String(describing: item) } else if let item = nameValue?.allValues[j] as? Int { aItem = String(describing: item) } if item == "name" { user.name = aItem } else if item == "money" { user.money = aItem } else if item == "balance" { user.balance = aItem } else if item == "status" { user.status = aItem } j += 1 } let dateFormatter = DateFormatter() dateFormatter.dateFormat = "MM-dd-yyyy" if user.dateponents(separatedBy: "-")[0] == dateFormatter.string(from: Date())ponents(separatedBy: "-")[0] { self.sortedTableArray.append(user) self.sortedTableArray.sort(by: { (object1, object2) -> Bool in if object1.date == object2.date { return object1.id < object2.id } else { return object1.date < object2.date } }) } self.tableArray.append(user) self.tableArray.sort(by: { (object1, object2) -> Bool in if object1.date == object2.date && object1.year == object2.year { return object2.id > object1.id } else { return object1.date < object2.date || object1.year < object2.year } }) self.tableView.reloadData() } counter += 1 }) { (error) in print(error.localizedDescription) } i += 1 } } } } else { self.view.makeToast(message: "No Users Found in Database") } }) { (error) in print(error.localizedDescription) }

推荐答案

在您编写时,它的排序绝对正确.

It's sorting absolutely correct as you write it.

从一开始就比较每个字符的工作的字符串.如果第一个字符相等,则检查第二个,依此类推.在您的结果中,"10"<"2",导致"1"字符的unicode小于"2"字符的unicode.

Strings comparing work for every char from the beginning. If first char is equal then check second and so on. In your results "10" < "2", cause unicode of "1"-character is less then "2"-character code.

您需要像这样比较:

self.sortedTableArray.sort(by: { if $0.date != $1.date { return $0.date < $1.date } else { return Int($0.id) ?? 0 < Int($1.id) ?? 0 } })

此外,您还应该将日期与 Date (而非字符串)进行比较.

Also your should compare dates as Date not Strings.

更多推荐

数组无法正确排序:Swift 4

本文发布于:2023-10-28 02:35:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1535257.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   正确   Swift

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!