-2

I have an array of dictionaries:

LeaderBoard = [{'Driver':'Sebastian Williams', 'Car':1, 'Team':'Red Drink','Grid':2, 'Fastest Lap':'1:37.481','Race Time':'2:27:45.958','Points':10},
                   {'Driver':'Tom Hamilton' ,'Car':44 ,'Team':'Mercidas' ,'Grid':6 ,'Fastest Lap':'1:37.176' ,'Race Time':'2:26:52.094' ,'Points':25 },
                   {'Driver':'Danny Ricardo' ,'Car':3 ,'Team':'Red Drink' ,'Grid':8 ,'Fastest Lap':'1:38.459' ,'Race Time':'2:27:38.589' ,'Points':15 },
                   {'Driver':'Walter Borras' ,'Car':77 ,'Team':'Lewis' ,'Grid':14 ,'Fastest Lap':'1:38.264' ,'Race Time':'2:27:22.229' ,'Points':18 },
                   {'Driver':'Fernando Sonal' ,'Car':14 ,'Team':'Farrori' ,'Grid':16 ,'Fastest Lap':'1:38.587' ,'Race Time':'2:27:52.040' ,'Points':8 },
                   {'Driver':'Jeson Smith' ,'Car':22 ,'Team':'McMilan' ,'Grid':3 ,'Fastest Lap':'1:38.284' ,'Race Time':'2:27:39.484' ,'Points':12},]

How do I sort them in order of quickest 'Race Time'?

I have tried everything I can think of. I used replace and split to make the race times individual numbers that I could compare easily, but I couldn't get the loops working.

This is for homework, where we need to use a sorting algorithm we have learned. Using Python's sort or sorted is not allowed.

No other restrictions --- any way of sorting that array of dictionaries will be fine.

Kevin J. Chase
  • 3,856
  • 4
  • 21
  • 43
BRuce W
  • 9
  • 3
  • Use `array[0]['d'] > array[1]['d']` – Martin Konecny Nov 26 '15 at 20:16
  • Hi, read [this](http://stackoverflow.com/help/how-to-ask) to learn more about asking questions in SO. – rll Nov 26 '15 at 20:19
  • What do you want your output to be? Dictionaries are not ordered data structures, so sorting their contents makes no sense. Maybe a list for each unique key? Something else? – Kevin J. Chase Nov 26 '15 at 21:22
  • Please [edit] your question include _all_ restrictions your teacher has placed on this assignment. Playing "guess the spec" in comments is no fun for anyone. – Kevin J. Chase Nov 26 '15 at 22:26

2 Answers2

1

You can use sorted with the key argument.

array = sorted(array,key= lambda x: x['d'])
thomas
  • 1,773
  • 10
  • 14
0

Since you have to use your own sorting code, most answers on this site won't help you --- they're generally based on the sort or sorted functions that come with Python.

You can sort these dictionaries using a slight variant of whatever sorting function you already have. Wherever your old code compares two items like:

if a < b:
    # swap a and b

...you'll instead compare values contained in those items:

attribute = 'Race Time'
if a[attribute] < b[attribute]:
    # swap a and b

Note that your "race times" are actually strings in some weird format that will require work to convert into comparable numbers. This will probably be worth writing as a separate function, which your sorting function would use in exactly the same places:

if race_time(a) < race_time(b):
    ...

...or even:

if is_faster(a, b):
    ...

For a more general solution, look up comparator functions, which most languages use for general-purpose sorting, and key functions, a more efficient variant used in modern versions of Python. ("How do I sort a list of dictionaries by values of the dictionary in Python?", mentioned by SuperBiasedMan in the comments, has many examples of using key functions.)

Community
  • 1
  • 1
Kevin J. Chase
  • 3,856
  • 4
  • 21
  • 43