0

I'm trying to sort my list of list with a.sort(key=lambda x: x[0]) or a.sort(). The first element of my date is the day and I need to sort by it.

a = [['1/8/2018', 71], ['10/8/2018', 76], ['11/8/2018', 6], ['12/8/2018', 60], ['13/8/2018', 81], ['14/8/2018', 74], ['15/8/2018', 7], ['16/8/2018', 77], ['17/8/2018', 81], ['18/8/2018', 69], ['19/8/2018', 66], ['2/8/2018', 81], ['20/8/2018', 74], ['21/8/2018', 88], ['22/8/2018', 92], ['23/8/2018', 90], ['24/8/2018', 84], ['25/8/2018', 66], ['26/8/2018', 64], ['27/8/2018', 82], ['28/8/2018', 82], ['29/8/2018', 79], ['3/8/2018', 8], ['4/8/2018', 64], ['5/8/2018', 64], ['6/8/2018', 12], ['7/8/2018', 8], ['8/8/2018', 83], ['9/8/2018', 77]]

the results I need:

[['1/8/2018', 71],['2/8/2018', 81], ['3/8/2018', 8], ['4/8/2018', 64], ['5/8/2018', 64], ['6/8/2018', 12],['7/8/2018', 8], ['8/8/2018', 83], ['9/8/2018', 77], ['10/8/2018', 76], ['11/8/2018', 6], ['12/8/2018', 60], ['13/8/2018', 81], ['14/8/2018', 74], ['15/8/2018', 7], ['16/8/2018', 77], ['17/8/2018', 81], ['18/8/2018', 69], ['19/8/2018', 66], ['20/8/2018', 74], ['21/8/2018', 88], ['22/8/2018', 92], ['23/8/2018', 90], ['24/8/2018', 84], ['25/8/2018', 66], ['26/8/2018', 64], ['27/8/2018', 82], ['28/8/2018', 82], ['29/8/2018', 79]]
1
  • 1
    Looks like your current code is comparing those dates as a string instead of numbers or dates. Try to convert that into date than do your stuff. Commented Aug 30, 2018 at 13:39

2 Answers 2

3

Use datetime.datetime.strptime

Ex:

import datetime
a = [['1/8/2018', 71], ['10/8/2018', 76], ['11/8/2018', 6], ['12/8/2018', 60], ['13/8/2018', 81], ['14/8/2018', 74], ['15/8/2018', 7], ['16/8/2018', 77], ['17/8/2018', 81], ['18/8/2018', 69], ['19/8/2018', 66], ['2/8/2018', 81], ['20/8/2018', 74], ['21/8/2018', 88], ['22/8/2018', 92], ['23/8/2018', 90], ['24/8/2018', 84], ['25/8/2018', 66], ['26/8/2018', 64], ['27/8/2018', 82], ['28/8/2018', 82], ['29/8/2018', 79], ['3/8/2018', 8], ['4/8/2018', 64], ['5/8/2018', 64], ['6/8/2018', 12], ['7/8/2018', 8], ['8/8/2018', 83], ['9/8/2018', 77]]
a.sort(key=lambda x: datetime.datetime.strptime(x[0], "%d/%m/%Y"))
print(a)

Output:

[['1/8/2018', 71], ['2/8/2018', 81], ['3/8/2018', 8], ['4/8/2018', 64], ['5/8/2018', 64], ['6/8/2018', 12], ['7/8/2018', 8], ['8/8/2018', 83], ['9/8/2018', 77], ['10/8/2018', 76], ['11/8/2018', 6], ['12/8/2018', 60], ['13/8/2018', 81], ['14/8/2018', 74], ['15/8/2018', 7], ['16/8/2018', 77], ['17/8/2018', 81], ['18/8/2018', 69], ['19/8/2018', 66], ['20/8/2018', 74], ['21/8/2018', 88], ['22/8/2018', 92], ['23/8/2018', 90], ['24/8/2018', 84], ['25/8/2018', 66], ['26/8/2018', 64], ['27/8/2018', 82], ['28/8/2018', 82], ['29/8/2018', 79]]
Sign up to request clarification or add additional context in comments.

1 Comment

2

You can use str.split:

a = [['1/8/2018', 71], ['10/8/2018', 76], ['11/8/2018', 6], ['12/8/2018', 60], ['13/8/2018', 81], ['14/8/2018', 74], ['15/8/2018', 7], ['16/8/2018', 77], ['17/8/2018', 81], ['18/8/2018', 69], ['19/8/2018', 66], ['2/8/2018', 81], ['20/8/2018', 74], ['21/8/2018', 88], ['22/8/2018', 92], ['23/8/2018', 90], ['24/8/2018', 84], ['25/8/2018', 66], ['26/8/2018', 64], ['27/8/2018', 82], ['28/8/2018', 82], ['29/8/2018', 79], ['3/8/2018', 8], ['4/8/2018', 64], ['5/8/2018', 64], ['6/8/2018', 12], ['7/8/2018', 8], ['8/8/2018', 83], ['9/8/2018', 77]]
new_a = sorted(a, key=lambda x:[int(i) for i in x[0].split('/')])

Output:

[['1/8/2018', 71], ['2/8/2018', 81], ['3/8/2018', 8], ['4/8/2018', 64], ['5/8/2018', 64], ['6/8/2018', 12], ['7/8/2018', 8], ['8/8/2018', 83], ['9/8/2018', 77], ['10/8/2018', 76], ['11/8/2018', 6], ['12/8/2018', 60], ['13/8/2018', 81], ['14/8/2018', 74], ['15/8/2018', 7], ['16/8/2018', 77], ['17/8/2018', 81], ['18/8/2018', 69], ['19/8/2018', 66], ['20/8/2018', 74], ['21/8/2018', 88], ['22/8/2018', 92], ['23/8/2018', 90], ['24/8/2018', 84], ['25/8/2018', 66], ['26/8/2018', 64], ['27/8/2018', 82], ['28/8/2018', 82], ['29/8/2018', 79]]

2 Comments

Or actually convert the strings to dates to make the code less cryptic.
This will sort by day then by month then by year.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.