How to Sort Dates in a Dictionary

How to sort by dates from a dictionary I created?

The following line is not doing anything in your code:

[value for (key, value) in sorted(dict.items())]

This is a list comprehension, used to build lists, and returns a list. If you change your main to

def main():
dict=constructDictionary()
alist = [value for (key, value) in sorted(dict.items())]
print(aList)

Then your code will print a list containing the values of the dictionary, sorted by key. It will be sorted by the dictionary keys, since you used dict.items() and when sorting tuples, sorted will first compare the first element of the tuples, then the second, etc.

If you want to sort by the values in the dictionary, just change the list comprehension to

alist = list(sorted(dict.values()))

One tip: Python isn't Java. It's not recommended to use getters and setters. Look into property for that or just directly access the variables, if you are not interested in controlling an invariant or anything like that.

Also, instead of manually closing the file, you can use a context manager.

sort by values in dictionary when it is a date

You can use sorted with a custom function, followed by a for loop:

def sort_key(x):
return list(map(int, x[1].split('/')))[::-1]

res = sorted(dictionary.items(), key=sort_key)

for name, date in res:
print(name, date)

Mike 16/1
Betty 1/5
John 12/6

The critical bit is the sort_key function, which splits by '/', converts strings to integers, and reverses the list so month has priority over day.

sorting a list of dictionary values by date in python

You can do it this way:

list.sort(key=lambda item:item['date'], reverse=True)

How do you sort dates in a dictionary?

edit/update: Xcode 8.2.1 • Swift 3.0.2

extension String {
static let shortDateUS: DateFormatter = {
let formatter = DateFormatter()
formatter.calendar = Calendar(identifier: .iso8601)
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateStyle = .short
return formatter
}()
var shortDateUS: Date? {
return String.shortDateUS.date(from: self)
}
}

let dayTotalDicTest: [String:Int] = [
"04-09-2015" : 4,
"04-10-2015" : 6,
"04-07-2015" : 8,
"03-28-2015" : 10,
"12-10-2014" : 12,
"12-10-2015" : 12]

let myArrayOfTuples = dayTotalDicTest.sorted{
guard let d1 = $0.key.shortDateUS, let d2 = $1.key.shortDateUS else { return false }
return d1 < d2
}

print(myArrayOfTuples) // [("12-10-2014", 12), ("03-28-2015", 10), ("04-07-2015", 8), ("04-09-2015", 4), ("04-10-2015", 6), ("12-10-2015", 12)]\n"

for tuple in myArrayOfTuples {
print(tuple)
}

How to sort a list of dictionary based on date as key

I love using dateutil to parse dates, as it is a no-brainer for the job:

import dateutil.parser

sorted(data, key=lambda _dict: dateutil.parser.parse(list(_dict.keys())[0]), reverse=True)

The problem you have is to sort with a dict_keys object, which is not sortable. You need to convert that back into a string and then parse it into dates so that you can correctly compare one date with another.

Sort Dictionary by Key (Date)

Dictionaries are unordered by definition.

You could map the dictionary to a (array of) struct which can be sorted

struct Section {
let date : Date
let notes : [String]
}

let sections = groupedDictionary.map{Section(date: $0.key, notes: $0.value}.sorted{$0.date < $1.date}

Sort Python dict by datetime value

You could sort the keys like this:

sorted(dct, key=dct.get)

See the Sorting Mini-HOW TO for an explanation of this and other techniques for sorting.



Related Topics



Leave a reply



Submit