0

I need some help with sorting a list of dates. I have a few lines of code and I tried a lot of different solutions I found on the internet but none of them seems to work.

import datetime

class WhiteChocolate:
    def __init__(self, vervaldatum):
        self.vervaldatum = vervaldatum


WhiteChocoStock = []

WhiteChocoStock.append(WhiteChocolate("22/04/2014"))
WhiteChocoStock.append(WhiteChocolate("21/04/2015"))
WhiteChocoStock.append(WhiteChocolate("12/12/2013"))

test = sorted(WhiteChocoStock, key=lambda x: datetime.datetime.strptime(x, '%d/%m/%Y'))

I don't know what I'm doing wrong.

1
  • 1
    Are you getting an error, or it just give an unexpected result? Commented Dec 28, 2013 at 18:44

1 Answer 1

1

You are trying to sort a list of objects, when you need to sort by the vervaldatum property of each of the objects in your list, which contains the date string:

sorted(WhiteChocoStock, key=lambda x: datetime.datetime.strptime(x.vervaldatum, '%d/%m/%Y'))
Sign up to request clarification or add additional context in comments.

Comments

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.