0

I am trying to sort the data based on the year object here in this case. I would like to sort the data ascending based on the year in this case.

My list of list looks like this -

new_trades = [["PEAR", "s", 300, 890.08, "2020-10-10"],
     ["PEAR", "b", 300, 890.08, "2021-10-10"],
      ["PEAR", "b", 300, 890.08, "2001-10-10"],
     ["IBN", "s", 400, 890.08, "2020-10-10"],
     ["ABC", "s", 400, 890.08, "2010-10-10"],
         ["XYZ", "b", 400, 890.08, "2010-10-10"]]
vascus
  • 1
  • 1
  • 1
    If those are [iso-format dates](https://docs.python.org/3/library/datetime.html#datetime.date.isoformat) (i.e. year-month-day), they will sort naturally. So you can simply do `new_trades.sort(key=lambda x: x[4])`. – ekhumoro Oct 01 '22 at 10:58

1 Answers1

0

try this:

from datetime import datetime
new_trades.sort(key=lambda date: datetime.strptime(date[4], "%Y-%m-%d")) #index number of our date data is 4

Bushmaster
  • 4,196
  • 3
  • 8
  • 28