54 lines
2.0 KiB
Python
54 lines
2.0 KiB
Python
![]() |
import json
|
||
|
|
||
|
# lees json bestand en maak de tabel structuur aan, data_tpl
|
||
|
with open('steam.json') as file:
|
||
|
# laad de json in een list
|
||
|
blob = json.load(file)
|
||
|
|
||
|
# lege lijst aanmaken
|
||
|
temp_biglst = []
|
||
|
|
||
|
# voor elke item in de list
|
||
|
for i in blob:
|
||
|
# voeg elke regel in de item toe aan een tijdelijke list
|
||
|
temp_lst = [i['appid'], i['name'], i['release_date'], i['english'], i['developer'], i['publisher'],
|
||
|
i['platforms'], i['required_age'], i['categories'], i['genres'], i['steamspy_tags'],
|
||
|
i['achievements'], i['positive_ratings'], i['negative_ratings'], i['average_playtime'],
|
||
|
i['median_playtime'], i['owners'], i['price']]
|
||
|
|
||
|
# zet de tijdelijke lijst om in een tuple
|
||
|
temp_tpl = tuple(temp_lst)
|
||
|
# voeg de tijdelijke list toe in de datalijst
|
||
|
temp_biglst.append(temp_tpl)
|
||
|
|
||
|
# datalijst omzetten in tuple, dit is de globale variabele die opgeroepen mag worden
|
||
|
data_tpl = tuple(temp_biglst)
|
||
|
|
||
|
|
||
|
def sorteer(tpl_in, colnum, reverse=False):
|
||
|
"""
|
||
|
:param tpl_in: geneste tuple in
|
||
|
:param colnum: sorteren op basis van colom nummer vanaf 0
|
||
|
:param reverse: True of False, niet verplicht, standaard False
|
||
|
:return: een gesorteerde tuple
|
||
|
"""
|
||
|
lst_to_sort = list(tpl_in)
|
||
|
|
||
|
# dubbel for loop yay
|
||
|
for _ in range(len(lst_to_sort) - 1):
|
||
|
for x in range(len(lst_to_sort) - 1):
|
||
|
if reverse:
|
||
|
# als tuple+1 hoger dan x, swap plaatsen
|
||
|
if lst_to_sort[x][colnum] < lst_to_sort[x + 1][colnum]:
|
||
|
lst_to_sort[x + 1], lst_to_sort[x] = lst_to_sort[x], lst_to_sort[x + 1]
|
||
|
else:
|
||
|
# zelfde als hierboven maar dan andersom
|
||
|
if lst_to_sort[x][colnum] > lst_to_sort[x + 1][colnum]:
|
||
|
lst_to_sort[x], lst_to_sort[x + 1] = lst_to_sort[x + 1], lst_to_sort[x]
|
||
|
|
||
|
# return een tuple terug
|
||
|
return tuple(lst_to_sort)
|
||
|
|
||
|
|
||
|
sorteer(data_tpl, 0)
|