Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading