title: How to sort items by date in tableview Swift 3
body: <p>So in my app, users can create expenses and store and view them in a per month basis. Currently, all my expense appear in the order I add them, but I want them to appear in the order of dates and I also want to have a tableviewheader with the dates so it groups the expenses together. I tried this code in my viewdidLoad. </p>
  
  <pre><code>for expense in monthlyExpenses{
          if let dateString = expense.modificationDate?.convertToString(){
          let dateFormatter = DateFormatter()
          dateFormatter.dateStyle = DateFormatter.Style.medium
          dateFormatter.timeStyle = DateFormatter.Style.none
          let finalDate = dateFormatter.date(from: dateString)
          if let finalDate = finalDate{
              let dateString2 = finalDate.timeIntervalSinceReferenceDate
              if self.toDoList[dateString2] == nil {
                  self.toDoList[dateString2] = [expense.name!]
              }
              else {
                  self.toDoList[dateString2]?.append(expense.name!)
              }
          }
      }
      }
  </code></pre>
  
  <p>and then tried this code in my tableviewheader section, however get a EXC_BAD_INSTRUCTION error. </p>
  
  <pre><code>func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -&gt; String? {
  
      let dateDouble = self.sortedSections[section]
  
      let date = Date(timeIntervalSinceReferenceDate: dateDouble)
  
      let dateformatter = DateFormatter()
  
      dateformatter.dateStyle = DateFormatter.Style.medium
  
      dateformatter.timeStyle = DateFormatter.Style.none
  
      return dateformatter.string(from: date)
  
  }
  </code></pre>
  
  <p>Each expense is an individual object that is stored in core data and has properties such as name, amount, modification date and etc. I want them to be sorted by modification Date. How will I go about doing this? </p>