Home > database >  Sort multiple strings by dates
Sort multiple strings by dates

Time:11-05

[EDIT] Adding this at the end of my fetchData function did the trick :

//Sort the tableView by date and time if it's the same date
self.tasks.sort { if $0.date != $1.date {
                                return $0.date < $1.date
                            }
                            else {
                                return $0.hour < $1.hour
                            }
                        }

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"]
        
        

        

CodePudding user response:

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

  • Related