list of dictionaries in another list and sort it according to date without using sort
Question:
I have a list of dictionaries in another list, I want sort those lists of dictionaries according to the date but I can’t use sort function I don’t know how to access in list (May be date it is not in correct way)
I WANT TO KNOW HOW TO SORT SOME THING LIKE THIS OR HOW TO GET ACCESS TO THE "DATE"
dr = [
[{"name": "Tom", "age": 10,"group":"sdd","points":2,"date":"2022 3 10"},
{"name": "Mark", "age": 5,"group":"sdo","points":6,"date":"2022 3 10"},
{"name": "Pam", "age": 7,"group":"spp","points":4,"date":"2022 3 10"}],
[{"name": "Tom", "age": 10,"group":"sdd","points":5,"date":"2022 4 12"},
{"name": "Mark", "age": 5,"group":"sdo","points":6,"date":"2022 4 12"},
{"name": "Pam", "age": 7,"group":"spp","points":6,"date":"2022 4 12"}],
[{"name": "Tom", "age": 10,"group":"sdd","points":8,"date":"2022 1 10"},
{"name": "Mark", "age": 5,"group":"sdo","points":12,"date":"2022 1 10"},
{"name": "Pam", "age": 7,"group":"spp","points":6,"date":"2022 1 10"}],
]
I tried like this, but it doesn’t work
for j in range(len(dr)):
for k in range(j+1,len(dr)):
if dr[j]["date"] < dr[k]["date"]:
dr[j],dr[k]=dr[k],dr[j]
after sorting it should be like this,
dr = [
[{"name": "Tom", "age": 10,"group":"sdd","points":8,"date":"2022 1 10"},
{"name": "Mark", "age": 5,"group":"sdo","points":12,"date":"2022 1 10"},
{"name": "Pam", "age": 7,"group":"spp","points":6,"date":"2022 1 10"}],
[{"name": "Tom", "age": 10,"group":"sdd","points":2,"date":"2022 3 10"},
{"name": "Mark", "age": 5,"group":"sdo","points":6,"date":"2022 3 10"},
{"name": "Pam", "age": 7,"group":"spp","points":4,"date":"2022 3 10"}],
[{"name": "Tom", "age": 10,"group":"sdd","points":5,"date":"2022 4 12"},
{"name": "Mark", "age": 5,"group":"sdo","points":6,"date":"2022 4 12"},
{"name": "Pam", "age": 7,"group":"spp","points":6,"date":"2022 4 12"}]
]
Answers:
If the "date"
key was actually a date (which currently isn’t), this would have worked:
for j in range(len(dr)):
for k in range(j + 1, len(dr)):
if dr[j][0]["date"] < dr[k][0]["date"]:
dr[j], dr[k] = dr[k], dr[j]
Your problem is, because dr
is a list of list of dictionaries (i.e. not a list of dictionaries) you have to compare the first date
in the list (assuming that all dates within one sublist is equal).
You must be getting an error from attempting to reference a date
key from the inner list objects. What you want to do is first grab the first dictionary from each inner list, using [0]
, and then reference and compare the date
key within that dictionary. Also, your use of <
will give you a reverse sort. Use >
to get the order you show being what you desire. This works:
for j in range(len(dr)):
for k in range(j+1,len(dr)):
if dr[j][0]["date"] > dr[k][0]["date"]:
dr[j],dr[k]=dr[k],dr[j]
Note that this only works after adding quotes around your date strings, and then only works because those strings happen to do the right thing when compared as simple strings. For example, if you had a 2 digit month, I think your sorting wouldn’t be right. You should really have datetime
objects in your structure and be comparing those.
I have a list of dictionaries in another list, I want sort those lists of dictionaries according to the date but I can’t use sort function I don’t know how to access in list (May be date it is not in correct way)
I WANT TO KNOW HOW TO SORT SOME THING LIKE THIS OR HOW TO GET ACCESS TO THE "DATE"
dr = [
[{"name": "Tom", "age": 10,"group":"sdd","points":2,"date":"2022 3 10"},
{"name": "Mark", "age": 5,"group":"sdo","points":6,"date":"2022 3 10"},
{"name": "Pam", "age": 7,"group":"spp","points":4,"date":"2022 3 10"}],
[{"name": "Tom", "age": 10,"group":"sdd","points":5,"date":"2022 4 12"},
{"name": "Mark", "age": 5,"group":"sdo","points":6,"date":"2022 4 12"},
{"name": "Pam", "age": 7,"group":"spp","points":6,"date":"2022 4 12"}],
[{"name": "Tom", "age": 10,"group":"sdd","points":8,"date":"2022 1 10"},
{"name": "Mark", "age": 5,"group":"sdo","points":12,"date":"2022 1 10"},
{"name": "Pam", "age": 7,"group":"spp","points":6,"date":"2022 1 10"}],
]
I tried like this, but it doesn’t work
for j in range(len(dr)):
for k in range(j+1,len(dr)):
if dr[j]["date"] < dr[k]["date"]:
dr[j],dr[k]=dr[k],dr[j]
after sorting it should be like this,
dr = [
[{"name": "Tom", "age": 10,"group":"sdd","points":8,"date":"2022 1 10"},
{"name": "Mark", "age": 5,"group":"sdo","points":12,"date":"2022 1 10"},
{"name": "Pam", "age": 7,"group":"spp","points":6,"date":"2022 1 10"}],
[{"name": "Tom", "age": 10,"group":"sdd","points":2,"date":"2022 3 10"},
{"name": "Mark", "age": 5,"group":"sdo","points":6,"date":"2022 3 10"},
{"name": "Pam", "age": 7,"group":"spp","points":4,"date":"2022 3 10"}],
[{"name": "Tom", "age": 10,"group":"sdd","points":5,"date":"2022 4 12"},
{"name": "Mark", "age": 5,"group":"sdo","points":6,"date":"2022 4 12"},
{"name": "Pam", "age": 7,"group":"spp","points":6,"date":"2022 4 12"}]
]
If the "date"
key was actually a date (which currently isn’t), this would have worked:
for j in range(len(dr)):
for k in range(j + 1, len(dr)):
if dr[j][0]["date"] < dr[k][0]["date"]:
dr[j], dr[k] = dr[k], dr[j]
Your problem is, because dr
is a list of list of dictionaries (i.e. not a list of dictionaries) you have to compare the first date
in the list (assuming that all dates within one sublist is equal).
You must be getting an error from attempting to reference a date
key from the inner list objects. What you want to do is first grab the first dictionary from each inner list, using [0]
, and then reference and compare the date
key within that dictionary. Also, your use of <
will give you a reverse sort. Use >
to get the order you show being what you desire. This works:
for j in range(len(dr)):
for k in range(j+1,len(dr)):
if dr[j][0]["date"] > dr[k][0]["date"]:
dr[j],dr[k]=dr[k],dr[j]
Note that this only works after adding quotes around your date strings, and then only works because those strings happen to do the right thing when compared as simple strings. For example, if you had a 2 digit month, I think your sorting wouldn’t be right. You should really have datetime
objects in your structure and be comparing those.