-1

This is the JSON:

[{'can_occur_before': False,
  'categories': [{'id': 8, 'name': 'Airdrop'}],
  'coins': [{'id': 'cashaa', 'name': 'Cashaa', 'symbol': 'CAS'}],
  'created_date': '2018-05-26T03:34:05+01:00',
  'date_event': '2018-06-05T00:00:00+01:00',
  'title': 'Unsold Token Distribution',
  'twitter_account': None,
  'vote_count': 125},
 {'can_occur_before': False,
  'categories': [{'id': 4, 'name': 'Exchange'}],
  'coins': [{'id': 'tron', 'name': 'TRON', 'symbol': 'TRX'}],
  'created_date': '2018-06-04T03:54:59+01:00',
  'date_event': '2018-06-05T00:00:00+01:00',
  'title': 'Indodax Listing',
  'twitter_account': '@PutraDwiJuliyan',
  'vote_count': 75},
 {'can_occur_before': False,
  'categories': [{'id': 5, 'name': 'Conference'}],
  'coins': [{'id': 'modum', 'name': 'Modum', 'symbol': 'MOD'}],
  'created_date': '2018-05-26T03:18:03+01:00',
  'date_event': '2018-06-05T00:00:00+01:00',
  'title': 'SAPPHIRE NOW',
  'twitter_account': None,
  'vote_count': 27},
 {'can_occur_before': False,
  'categories': [{'id': 4, 'name': 'Exchange'}],
  'coins': [{'id': 'apr-coin', 'name': 'APR Coin', 'symbol': 'APR'}],
  'created_date': '2018-05-29T17:45:16+01:00',
  'date_event': '2018-06-05T00:00:00+01:00',
  'title': 'TopBTC Listing',
  'twitter_account': '@cryptoalarm',
  'vote_count': 23}]

I want to take all the date_events and append them to a list in chronological order. I currently have this code and am not sure how to order them chronologically.

date = []
for i in getevents:
    date.append(i['date_event'][:10])

Thanks for any help !

Ilya
  • 183
  • 5
xvienxz2
  • 53
  • 1
  • 5
  • The timezone information *is* important. But consider parsing the datetimes correctly, using for example, [dateutil](http://labix.org/python-dateutil). – jedwards Jun 04 '18 at 23:20
  • 1
    Use the datetime package. Convert each event time stamp to a datetime item, then simply sort them. – Prune Jun 04 '18 at 23:23

2 Answers2

0

Simple way is to compose a list and then apply sort() method

data = json.load(open('filename.json','r'))
dates = [item['date_event'] for i in data]
dates.sort()

Using your example data with field 'creation_date' ('date_event' values are all the same) we'll get:

['2018-05-26T03:18:03+01:00',
'2018-05-26T03:34:05+01:00',
'2018-05-29T17:45:16+01:00',
'2018-06-04T03:54:59+01:00']
Ilya
  • 183
  • 5
-1

First of all, all the date_event in your array of objects are all the same, so not much sense in sorting them.. Also your approach will not get you far, you need to convert the dates to native date/time objects so that you can sort them through a sorting function.

The easiest way to parse properly formatted Date/Times is to use dateutil.parse.parser, and sorting an existing list is done by list.sort() - I made a quick example on how to use these tools, also i changed the date_event values to showcase it: https://repl.it/repls/BogusSpecificRate

After you have decoded the JSON string (json.loads) and have a Python list to work with, you can proceed with sorting the list:

# Ascending
events.sort(key=lambda e: parser.parse(e['date_event']))
print([":".join([e['title'], e['date_event']]) for e in events])

# Descending
events.sort(key=lambda e: parser.parse(e['date_event']), reverse=True)
print([":".join([e['title'], e['date_event']]) for e in events])
wiesion
  • 2,349
  • 12
  • 21