RBLX CLICK
RBLX CLICK

Reputation: 33

Sort on value (top 5) in multidimensional array

I have the following list:

[{'id': 610603160301469715, 'worth': 0.17},
 {'id': 242730576195354624, 'worth': 0.0},
 {'id': 659311972990451712, 'worth': 0.0},
 {'id': 365975655608745985, 'worth': 0.0},
 {'id': 472141928578940958, 'worth': 0.0},
 {'id': 651499863736844298, 'worth': 0.02},
 {'id': 270904126974590976, 'worth': 0.0},
 {'id': 280497242714931202, 'worth': 0.0},
 {'id': 432610292342587392, 'worth': 0.0},
 {'id': 408785106942164992, 'worth': 0.0},
 {'id': 400900758444310528, 'worth': 0.04},
 {'id': 602213380274651400, 'worth': 0.06},
 {'id': 622692428213780481, 'worth': 0.0}]

It contains items with the id and worth. Is there any way to sort by worth? I don't really know what keywords I need to search. So that's why I'm asking here at the moment. I did try to search, but couldn't really find it.

Upvotes: 2

Views: 78

Answers (2)

Ch3steR
Ch3steR

Reputation: 20669

You can also use itemgetter.

from operator import itemgetter
sorted(l,key=itemgetter('worth'))

[{'id': 242730576195354624, 'worth': 0.0},
 {'id': 659311972990451712, 'worth': 0.0},
 {'id': 365975655608745985, 'worth': 0.0},
 {'id': 472141928578940958, 'worth': 0.0},
 {'id': 270904126974590976, 'worth': 0.0},
 {'id': 280497242714931202, 'worth': 0.0},
 {'id': 432610292342587392, 'worth': 0.0},
 {'id': 408785106942164992, 'worth': 0.0},
 {'id': 622692428213780481, 'worth': 0.0},
 {'id': 651499863736844298, 'worth': 0.02},
 {'id': 400900758444310528, 'worth': 0.04},
 {'id': 602213380274651400, 'worth': 0.06},
 {'id': 610603160301469715, 'worth': 0.17}]

To get 5 largest values try this.

top_five=sorted(l,key=itemgetter('worth'))[-5:][::-1]

[{'id': 610603160301469715, 'worth': 0.17},
 {'id': 602213380274651400, 'worth': 0.06},
 {'id': 400900758444310528, 'worth': 0.04},
 {'id': 651499863736844298, 'worth': 0.02},
 {'id': 622692428213780481, 'worth': 0.0}]

If you want to sort in-place then use .sort() method with key=itemgetter('worth')

l.sort(key=itemgetter('worth')) # .sort() doen't return None. And it modifies the same list.

If you are sorting to only get the top 5 largest then. Using heapq doesn't require sorting.

from heapq import nlargest
top_five=nlargest(5,l,key=itemgetter('worth'))

[{'id': 610603160301469715, 'worth': 0.17},
 {'id': 602213380274651400, 'worth': 0.06},
 {'id': 400900758444310528, 'worth': 0.04},
 {'id': 651499863736844298, 'worth': 0.02},
 {'id': 242730576195354624, 'worth': 0.0}]

Upvotes: 2

CDJB
CDJB

Reputation: 14516

You can use sorted() with a lambda expression as the key:

>>> sorted(l, key=lambda x: x['worth'])
[{'id': 242730576195354624, 'worth': 0.0},
 {'id': 659311972990451712, 'worth': 0.0},
 {'id': 365975655608745985, 'worth': 0.0},
 {'id': 472141928578940958, 'worth': 0.0},
 {'id': 270904126974590976, 'worth': 0.0},
 {'id': 280497242714931202, 'worth': 0.0},
 {'id': 432610292342587392, 'worth': 0.0},
 {'id': 408785106942164992, 'worth': 0.0},
 {'id': 622692428213780481, 'worth': 0.0},
 {'id': 651499863736844298, 'worth': 0.02},
 {'id': 400900758444310528, 'worth': 0.04},
 {'id': 602213380274651400, 'worth': 0.06},
 {'id': 610603160301469715, 'worth': 0.17}]

To get the top 5 elements:

>>> sorted(l, key=lambda x: x['worth'], reverse=True)[:5]
[{'id': 610603160301469715, 'worth': 0.17},
 {'id': 602213380274651400, 'worth': 0.06},
 {'id': 400900758444310528, 'worth': 0.04},
 {'id': 651499863736844298, 'worth': 0.02},
 {'id': 242730576195354624, 'worth': 0.0}]

Upvotes: 1

Related Questions