Skip to content

Instantly share code, notes, and snippets.

@ig0r-ferreira
Created January 16, 2023 21:59
Show Gist options
  • Save ig0r-ferreira/d9a3e3daac811938c8af78d2574f8602 to your computer and use it in GitHub Desktop.
Save ig0r-ferreira/d9a3e3daac811938c8af78d2574f8602 to your computer and use it in GitHub Desktop.
from datetime import datetime
contracts = [
{
"id": 157289,
"id_cliente": 118801,
"nome": "FULANO DA SILVA",
"data_gerado": "2019-04-30"
},
{
"id": 142032,
"id_cliente": 118801,
"nome": "FULANO DA SILVA",
"data_gerado": "2020-09-04"
},
{
"id": 269493,
"id_cliente": 118805,
"nome": "RANGEL LUIZ DOS SANTOS",
"prazo": 12,
"data_gerado": "2021-10-22"
},
{
"id": 146051,
"id_cliente": 118805,
"nome": "RANGEL LUIZ DOS SANTOS",
"prazo": 12,
"data_gerado": "2016-03-05"
},
{
"id": 145356,
"id_cliente": 118805,
"nome": "RANGEL LUIZ DOS SANTOS",
"prazo": 12,
"data_gerado": "2018-03-05"
}
]
print(sorted(contracts, key=lambda contrato: datetime.strptime(contrato['data_gerado'], '%Y-%M-%d')).pop())
from datetime import datetime
from operator import itemgetter
def convert_to_datetime(string):
return datetime.strptime(string, '%Y-%M-%d')
def as_datetime(function):
def wrapper(obj):
return convert_to_datetime(function(obj))
return wrapper
contracts = [
{
"id": 157289,
"id_cliente": 118801,
"nome": "FULANO DA SILVA",
"data_gerado": "2019-04-30"
},
{
"id": 142032,
"id_cliente": 118801,
"nome": "FULANO DA SILVA",
"data_gerado": "2020-09-04"
},
{
"id": 269493,
"id_cliente": 118805,
"nome": "RANGEL LUIZ DOS SANTOS",
"prazo": 12,
"data_gerado": "2021-10-22"
},
{
"id": 146051,
"id_cliente": 118805,
"nome": "RANGEL LUIZ DOS SANTOS",
"prazo": 12,
"data_gerado": "2016-03-05"
},
{
"id": 145356,
"id_cliente": 118805,
"nome": "RANGEL LUIZ DOS SANTOS",
"prazo": 12,
"data_gerado": "2018-03-05"
}
]
get_contract_date = as_datetime(itemgetter('data_gerado'))
print(sorted(contracts, key=get_contract_date).pop())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment