2

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!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
kingkps
  • 156
  • 1
  • 13
  • Why is your date a string with a separate year and not a `Date`? If you used the right data type then your sorting would work correctly – Paulw11 Jan 06 '18 at 02:16
  • This is because I need the date to be displayed to the user in a specific form. – kingkps Jan 06 '18 at 05:04
  • That is the job of `NSDateFormatter`; not how you store the date – Paulw11 Jan 06 '18 at 06:49
  • The reason I did this is because I need to store the date in the database, and this was the best way for me to do it. – kingkps Jan 07 '18 at 04:17
  • If your database doesn’t have a Date type then you should store your dates as RFC3339 strings. You can then easily convert the string to a Swift `Date` when you fetch the data – Paulw11 Jan 07 '18 at 04:43

1 Answers1

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 years of the objects are compared, followed by their dates, making objects of earlier year come before the later years.

Ideally, these struct fields should not be string types. These dates and years should be Date types, or at least logically, Ints.

Miket25
  • 1,895
  • 3
  • 15
  • 29
  • This anding and oring gets really complex as you introduce more and more sort criteria. See https://stackoverflow.com/a/37604010/3141234 – Alexander Jan 06 '18 at 03:16
  • Thanks for the code!! I didn’t know you could return one thing or another thing and I didn’t think to add the && part. Thanks! – kingkps Jan 06 '18 at 03:18
  • Hi Miket25, I tried out the code and it didn't work, the id sorting is now messed up and the 2018 entries show up above the 2017 ones. Can you help? – kingkps Jan 06 '18 at 05:15