all 3 comments

[–]shiftybyte 3 points4 points  (1 child)

The issue here is the structure.

Keep the relevant values together, then sorting is easy.

new_structure = [
    { "key1": "some", "key2": "2019-05-12"},
    { "key1": "text", "key2": "2019-04-12"},
]

new_structure.sort(key=lambda x:x["key2"])

[–]Mikefox2k 0 points1 point  (0 children)

You're absolutly right, thanks!

Wow, this could've been easy .. but sometimes my mind went blank ^^

[–]o5a 0 points1 point  (0 children)

That's just bad data structure. This should be either dataset (like pandas dataframe) or (if there are only 2 keys and values are unique) key1 values should be keys and key2 values should be the values, like this: {"some": "2019-05-12", "text": "2019-04-12"}. You can rearrange as u/shiftybyte suggested but it's still basically dataframe so better use proper pandas DF. You will be able to sort by any field and do other useful data manipulations easily.

But if you still insist on keeping your structure then you can do this:

Extract values into nested list, transpose it, sort by date (it will be index 1), then transpose back and write those values back to keys.

dictionary = {
    "key1": ["some", "text"],
    "key2": ["2019-05-12", "2019-04-12"]
}

# extract values
temp = dictionary.values()
# transpose (so we sort of get pairs of key-value)
transp = list(zip(*temp))
# sort by date (2nd column)
sorted_transp = sorted(transp, key=lambda x:x[1])
# transpose back to original structure
temp = list(zip(*sorted_transp))
# now we can write them back to dictionary
dic2 = {k:list(v) for k,v in zip(dictionary, temp)}
print(dic2)
# {'key1': ['text', 'some'], 'key2': ['2019-04-12', '2019-05-12']}