Sort a DefaultDict by value which is a list and then by key

30 Views Asked by At

I'm asking for you help here because i didn't find how to do it on internet and even after trying myself. Here is my concern: I want to create an Order Book for a trading strategy, I created then a dictionary like this :

bids = {Price : (size, submission_time, is_market_maker)}

When executing orders, the first priority is price, and, within a given price, the order that was sent prior to another one will have priority (sort by number, the lower being the first). This is called the price-priority rule for order books.

I already tried this function with the '[1][1]' to sort by the value which is a list but it doesn't work maybe because I'm using a DefaultDict I don't know

sorted_dict = sorted(bids.items(), key=lambda item: item[1][1])

And then I would like to sort it by price without changing the order of the orders.

I would like it to be like this :

bids = {100: [(5,2,True), (4,1, False)], 101: [(2,3,True), (4,1, False)]}
bids = {101: [(4, 1, False), (2 , 3, True)] , 100: [(4, 1, False), (5, 2, True)]}

Thank you for your help !

0

There are 0 best solutions below