1

I have table view, that have data like "from,To, Action, Date and description". now I'm getting the date as string format and I need to sort the messages by date from latest date to earlier date, I tried sort code but it sort just first two characters. for ex: tSFDATE have strings "11/01/2010", "01/02/2010". the result will be

"01/02/2010", "11/01/2010". so any solution to sort TrackObj by date

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

        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "dd/MM/yyyy - hh:mm:ss"


        let sortedArray = self.trackingListArr.sorted { dateFormatter.date(from: $0.tSFDATE!)! < dateFormatter.date(from: $1.tSFDATE!)! }


        let trackObj = sortedArray[indexPath.row]


            cell.lblFromValue.text = trackObj.fROMUSERAR!

            cell.lblToValue.text = trackObj.tOUSERAR!

            cell.lblPurpsValue.text = trackObj.tSFPURPOSEAR!

            cell.lblDateValue.text = trackObj.tSFDATE!

            cell.lblNoteValue.text = trackObj.tSFDESCRIPTION!

    return (cell)

}

here is the result image enter image description here

3
  • use .compare for date comparison instead. something like date1.compare(date2) == .orderedAscending Commented Jan 10, 2018 at 7:22
  • @koropok can you give me the code because I tried it but I believe there is something wrong with my code Commented Jan 10, 2018 at 7:29
  • Strings representing dates can only be sorted reliably if they are in format yyyy MM dd (first year then month then day). If this is not possible you have to convert the date string to Date. Commented Jan 10, 2018 at 8:22

1 Answer 1

7

You are trying to sort the string instead of the date. To properly sort it you first need to convert your tSFDATE string to Date and then sort it.

This should work with your existing array...

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy - HH:mm:ss"

let sortedArray = trackingListArr.sorted { (lhs: trackingListArr, rhs: trackingListArr) -> Bool in
    // you can have additional code here
    return dateFormatter.date(from: lhs.tSFDATE)! > dateFormatter.date(from: rhs.tSFDATE)!
}

EDIT : Compact closure declaration

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy - HH:mm:ss"

let sortedArray = trackingListArr.sorted { dateFormatter.date(from: $0.tSFDATE)! < dateFormatter.date(from: $1.tSFDATE)! }

EDIT 2 : Changed dateFormat to "dd/MM/yyyy - HH:mm:ss" to handle 24-hour time.

Sign up to request clarification or add additional context in comments.

10 Comments

what do you mean by example
Whoops. That was the struct I used to test my code. I've fixed it now and updated the answer.
That's strange, do you get an error or does it sort incorrectly?
I edited my question and attached your code and screenshot
I changed "<" to and it worked, but in some times it gives me the below error fatal error: unexpectedly found nil while unwrapping an Optional value. can you help me to submit this answer as a correct
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.