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).
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.
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:
- If the dates of two entries are the same, then they should be sorted by 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.
Thanks!