0

Anybody can help me with code or hints for sorting my tableView according to the date. I think i have to use NSDate. I have an array already ready with NSDate type instances. Thanks in advance.

Larme
  • 24,190
  • 6
  • 51
  • 81
Kegham K.
  • 1,589
  • 22
  • 40

2 Answers2

1

This post will most likely help you get what you are looking for.

How do I sort a swift array containing instances of NSManagedObject subclass by an attribute value (date)

Note: The tableview isn't what you want to sort. It is the array that you are using with the tableview that needs sorted.

Community
  • 1
  • 1
InSearchOf
  • 170
  • 4
1

If you have array of dates than you can sort by comparing the date, here is the example.....

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let dates = [
            NSDate(timeIntervalSince1970: 1453648103),
            NSDate(timeIntervalSince1970: 1453238129),
            NSDate(timeIntervalSince1970: 1453438129),
            NSDate(timeIntervalSince1970: 1453538129)
        ]
        print(dates)
        print(dates.sort({ $0.isLessThanDate($1) }))

    }
}
extension NSDate {

    func isLessThanDate(dateToCompare: NSDate) -> Bool {
        //Declare Variables
        var isLess = false

    //Compare Values
        if self.compare(dateToCompare) == NSComparisonResult.OrderedAscending {
            isLess = true
        }

        //Return Result
        return isLess
    }
}