skip to Main Content

I am working on code where I am receiving lots of data associated with dates
each object having one date parameter and there might many objects with the same date.

I need to show this all objects in UITableView. each object as one cell.
I succeed in that,
I need to get unique dates from the response array of objects.
Those unique dates will be stored in an array which will act as a number of sections of my table view with section header title will be the date from the unique date array.

somehow I am able to sort out that with what I want,
The only problem I am facing is I am not able to sort the unique date array
every time the sequence change.
I need the latest date as the first date and the oldest date as the end date.

How to achieve this in swift.

Following is a piece of code I have written

let sortedKeys = Array(dictValue.keys).sorted(by: {$0 > $1})
print(sortedKeys)

here dicValue.keys is my unique date array and I wanted to sort it.

Following is a sample response I am getting

["08/03/2021”, “10/02/2021”,  "26/04/2021", "25/03/2021”,  "09/12/2020”, , "27/04/2021”,  "23/03/2021”,  "11/01/2021”,  "05/03/2021”,  "09/03/2021”, "16/10/2020", "19/03/2021", "12/10/2020" ]

and after applying sort I am getting the following output

[“27/04/2021", "26/04/2021", "25/03/2021", "23/03/2021", "19/03/2021", "16/10/2020", "12/10/2020", "11/01/2021", "10/02/2021", "09/12/2020", "09/03/2021", "08/03/2021", "05/03/2021”]

where dates are not properly sorted out.
Can anyone please help me out with it.

Thanks in advance.

2

Answers


  1. This string date format is inappropriate for sorting, because the most significant component is day. Only a date format like yyyy/MM/dd can be sorted properly by comparison operator >.

    However this is Swift. The closure can contain anything as long as it returns a Bool. You could sort the array with a custom sort algorithm. It splits the strings into components and sorts first year then month then day

    let sortedKeys = dictValue.keys.sorted { (date1, date2) -> Bool in
        let comps1 = date1.components(separatedBy: "/")
        let comps2 = date2.components(separatedBy: "/")
        return (comps1[2], comps1[1], comps1[0]) > (comps2[2], comps2[1], comps2[0])
    }
    
    print(sortedKeys)
    
    Login or Signup to reply.
  2. If you want to sort a date, just sort a Date. Date supports Hashable and can be used as a dictionary key, you could map your original dictionary and by using a DateFormatter to format your string keys into Dates then you can easily sort them.

    let dictionary = ["08/03/2021": 2, "10/02/2021": 5,  "26/04/2021" : 6]
    let formatter = DateFormatter()
    formatter.dateFormat = "dd/MM/yyyy" // You should probably adjust other properties of the formatter
    
    let newDict = Dictionary(uniqueKeysWithValues:
                                dictionary.map { (key, value) -> (Date, Int) in
                                    print("Key: (key)")
                                    return (formatter.date(from: key)!, value)
                                })
    
    let sortedDates = newDict.keys.sorted { $0 > $1 }
    let value = newDict[sortedDates[0]]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search