This question already has an answer here:
这个问题已经有了答案:
- Swift - Sort array of objects with multiple criteria 8 answers
- Swift -对具有多个条件的对象数组进行排序
I made an app last year for someone so that they could have a track of the money that came in from their clients. At that time, I did not consider what would happen when there were entries in there that had different years and sure enough, when 2018 came, the sorting of the entries got messed up. I am unable to figure out how I can sort my array of entries with two years and would appreciate it if any of you could help me fix this. Below is the format of the specific page that I am having an issue with and the code.
我去年为某个人做了一个应用,这样他们就可以跟踪客户的资金。当时,我并没有考虑到,如果有不同年份的参赛作品,会发生什么。我不知道怎样用两年的时间来整理我的条目,如果你们有人能帮我解决这个问题,我将不胜感激。下面是我有问题的特定页面的格式和代码。
Format:
格式:
There is a TableView with the structure below that gets sorted by the later mentioned criteria. The sorting of the array this tableView uses is getting messed up due to there being entries from both 2017 and 2018 (The dates have the year at the end).
有一个具有以下结构的TableView,它根据后面提到的标准进行排序。这个tableView使用的数组的排序被打乱了,因为从2017年到2018年都有条目(日期是年底)。
Date | Name | Money | Balance
日期|名称|货币|余额
Code:
代码:
Model for the Array:
模型组:
struct BalanceUser {
var id = ""
var name = ""
var date = ""
var money = ""
var balance = ""
var status = ""
var year = ""
}
I added a new variable to my array structure, year, so that I can sort using that.
我在数组结构中添加了一个新的变量,即year,这样我就可以使用它进行排序。
Current Sorting Code:
当前的排序代码:
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.sortedTableArray.sort(by: { (object1, object2) -> Bool in
return object1.year < object2.year
})
Currently, with the code above, the entries with a year of 2018 show above the 2017 entries. I want it so that all of the entries are sorted with the following criteria:
目前,有了以上的代码,2018年的参赛作品就会出现在2017年的参赛作品之上。我希望所有的参赛作品都符合以下标准:
- If the dates of two entries are the same, then they should be sorted by id
- 如果两个条目的日期相同,那么它们应该按id排序
- If the dates of two entires are not the same, they should be sorted by date
- 如果两个实体的日期不相同,则应该按日期排序
- All entries should be sorted by year, so that the entries from 2018 show below the 2017 entries.
- 所有参赛作品应按年份排序,以便2018年参赛作品在2017年参赛作品下方显示。
Thanks!
谢谢!
1 个解决方案
#1
1
Your sort closure should also compare the years of the entries along with the dates.
排序闭包还应该比较条目的年份和日期。
self.sortedTableArray.sort(by: {(obj1, obj2) -> Bool in
if obj1.date == obj2.date && obj1.year == obj2.year {
return obj1.id < obj2.id
}
return obj1.year < obj2.year || obj1.date < obj2.date
})
Here, if the date
and year
of both object are the same, the id
is sorted. If they are not, the year
s of the objects are compared, followed by their dates, making objects of earlier year come before the later years.
这里,如果两个对象的日期和年份是相同的,则对id进行排序。如果不是,则比较对象的年份,然后再比较它们的日期,使较早年份的对象先于较晚年份。
Ideally, these struct fields should not be string types. These dates and years should be Date
types, or at least logically, Int
s.
理想情况下,这些结构域不应该是字符串类型。这些日期和年份应该是日期类型,或者至少在逻辑上应该是int类型。
#1
1
Your sort closure should also compare the years of the entries along with the dates.
排序闭包还应该比较条目的年份和日期。
self.sortedTableArray.sort(by: {(obj1, obj2) -> Bool in
if obj1.date == obj2.date && obj1.year == obj2.year {
return obj1.id < obj2.id
}
return obj1.year < obj2.year || obj1.date < obj2.date
})
Here, if the date
and year
of both object are the same, the id
is sorted. If they are not, the year
s of the objects are compared, followed by their dates, making objects of earlier year come before the later years.
这里,如果两个对象的日期和年份是相同的,则对id进行排序。如果不是,则比较对象的年份,然后再比较它们的日期,使较早年份的对象先于较晚年份。
Ideally, these struct fields should not be string types. These dates and years should be Date
types, or at least logically, Int
s.
理想情况下,这些结构域不应该是字符串类型。这些日期和年份应该是日期类型,或者至少在逻辑上应该是int类型。