Sort multiple strings by dates

I just started programming and i’m stuck, basically i have a tableview showing multiple tasks and i would like to sort the tasks by dates so i extracted the time/hour of the tasks (already created in the firebase) then converted them into Date to sort them but i don’t know how to "convert" them back to strings to put them back into my cell.date.text… Here’s why i tried so far :

var convertedArray: [Date] = []

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath) as! MyTasksTableCell

                //Gathering the date and hour (string) 
                let date = "\(Tasks[indexPath.row].date)"
                let hour = "\(Tasks[indexPath.row].hour)"
                let dateFormatter = DateFormatter()
                dateFormatter.dateFormat = "MM/dd/yyyy 'at' HH:mm"
                let DateHour = date + " at " + hour
                //OUTPUT:
                //  10/22/2021 at 15:10
                //  10/21/2021 at 22:24
                //  10/21/2021 at 01:55
                //  12/16/2021 at 14:50

                // Converting them to a Date
                let finalDate = dateFormatter.date(from: DateHour)
                //OUTPUT:
                //  Optional(2021-12-16 13:50:00 +0000)
                //  Optional(2021-10-22 13:10:00 +0000)
                //  Optional(2021-10-21 20:24:00 +0000)
                //  Optional(2021-10-20 23:55:00 +0000)
                
                //Sorting them
                convertedArray.append(finalDate!)
                let dateObject = convertedArray.sorted(by: { $0.compare($1) == .orderedAscending })                                                 
                //OUTPUT: 
                //  [2021-10-22 13:10:00 +0000]
                //  [2021-10-22 13:10:00 +0000, 2021-12-16 13:50:00 +0000]
                //  [2021-10-21 20:24:00 +0000, 2021-10-22 13:10:00 +0000, 2021-12-16 13:50:00 +0000]
                //  [2021-10-20 23:55:00 +0000, 2021-10-21 20:24:00 +0000, 2021-10-22 13:10:00 +0000, 2021-12-16 13:50:00 +0000]
        
        for date in dateObject {
            let date = dateFormatter.string(from: date)
                //OUTPUT:
                //  12/16/2021 at 14:50
                //  10/22/2021 at 15:10
                //  12/16/2021 at 14:50
                //  10/21/2021 at 01:55
                //  10/22/2021 at 15:10
                //  12/16/2021 at 14:50
                //  10/21/2021 at 01:55
                //  10/21/2021 at 22:24
                //  10/22/2021 at 15:10
                //  12/16/2021 at 14:50
        }
        
        var newList = [String]()
        for date in dateObject {
            let dateformatter =  DateFormatter()
            dateformatter.dateFormat = "MM/dd/yyyy 'at' HH:mm"
            let convertDate = dateformatter.string(from: date)
            newList.append(convertDate)
        }
        print(newList)
        //OUTPUT:
//        ["12/16/2021 at 14:50"]
//        ["10/22/2021 at 15:10", "12/16/2021 at 14:50"]
//        ["10/21/2021 at 22:24", "10/22/2021 at 15:10", "12/16/2021 at 14:50"]
//        ["10/21/2021 at 01:55", "10/21/2021 at 22:24", "10/22/2021 at 15:10", "12/16/2021 at 14:50"]
        
        

        

>Solution :

You have to order the dataSource (here your Tasks array). Then they will show in the correct order in TableView.

Or you could also create a computed var orderedTasks and use it as your dataSource.

Note: var names in Swift should start with lowerCase, so use tasks instead of Tasks (which would fit for a class or struct naming).

Leave a Reply