-4

I am trying to sort this JSON data by "algo". How can i do this? Lets say i want to sort by algo value x11 first. So that all the objects having this algo must be shown first.

I know simple sorting but i don't know where to start with this one. So i haven't tried anything yet. Please let me know if this is easily do able?

Here is the JSON data that i am trying to sort.

{
    "data": {
        "aby": {
            "info": {
                "algo": "scrypt",
                "bestexchange": 4,
                "bestPool": 11,
                "blocks": 1036871,
                "blocktime": 0,
                "coin": "aby",
                "coinsPerDay": "0.66582227581137665151",
                "diff": 5409.10761,
                "diffAlgo": "0.0058443642207875908",
                "hashAlgo": "0",
                "nethash": 113882.060223,
                "pos": "?",
                "priceBTC": "0.00000098",
                "priceUSD": "0.00877766399999999926",
                "reward": 200.037,
                "timestamp": "2018-05-10 20:56:25.949806",
                "type": "diff",
                "usdPerDay": "0.0058443642207875908",
                "value": "0.0058443642207875908",
                "workers": 311
            },
            "order": 129
        },
        "adz": {
            "info": {
                "algo": "x11",
                "bestexchange": "None",
                "bestPool": 10,
                "blocks": 422399,
                "blocktime": 0,
                "coin": "adz",
                "coinsPerDay": "0.22962182821550283784",
                "diff": 70085.3733,
                "diffAlgo": "0.00450653503837463755",
                "hashAlgo": "0",
                "nethash": 509330.602794,
                "pos": "?",
                "priceBTC": "0.0000022",
                "priceUSD": "0.01962590000000000168",
                "reward": 40,
                "timestamp": "2018-05-10 20:56:25.949806",
                "type": "diff",
                "usdPerDay": "0.00450653503837463755",
                "value": "0.00450653503837463755",
                "workers": 270
            },
            "order": 138
        },
        "agn": {
            "info": {
                "algo": "neoscrypt",
                "bestexchange": 1,
                "bestPool": 10,
                "blocks": 58768,
                "blocktime": 0,
                "coin": "agn",
                "coinsPerDay": "61.14166600687313746221",
                "diff": 3.75075034,
                "diffAlgo": "1.58813765486204783173",
                "hashAlgo": "0",
                "nethash": 67.403138,
                "pos": "?",
                "priceBTC": "0.0000029",
                "priceUSD": "0.02597471999999999975",
                "reward": 6,
                "timestamp": "2018-05-10 20:56:25.949806",
                "type": "diff",
                "usdPerDay": "1.58813765486204783173",
                "value": "1.58813765486204783173",
                "workers": 467
            },
            "order": 53
        },
        "agni": {
            "info": {
                "algo": "nist5",
                "bestexchange": 5,
                "bestPool": 10,
                "blocks": 28349,
                "blocktime": 0,
                "coin": "agni",
                "coinsPerDay": "1.04090749113355163225",
                "diff": 1720,
                "diffAlgo": "0.09341846617018165211",
                "hashAlgo": "0",
                "nethash": 57755.561016,
                "pos": "?",
                "priceBTC": "0.00001002",
                "priceUSD": "0.08974713600000000513",
                "reward": 1,
                "timestamp": "2018-05-10 20:56:25.949806",
                "type": "diff",
                "usdPerDay": "0.09341846617018165211",
                "value": "0.09341846617018165211",
                "workers": 1
            },
            "order": 92
        },
        "akn": {
            "info": {
                "algo": "neoscrypt",
                "bestexchange": 1,
                "bestPool": 3,
                "blocks": 38207,
                "blocktime": 0,
                "coin": "akn",
                "diff": 1,
                "diffAlgo": "0",
                "hashAlgo": "0",
                "nethash": 39.500835,
                "pos": "?",
                "priceBTC": "0.000005001",
                "priceUSD": "0.04479295679999999863",
                "reward": 1.4,
                "timestamp": "2018-05-10 20:56:25.949806",
                "type": "not enough data",
                "value": "0",
                "workers": 6
            },
            "order": 68
        },
        "alps": {
            "info": {
                "algo": "lyra2z",
                "bestexchange": 1,
                "bestPool": 10,
                "blocks": 33645,
                "blocktime": 0,
                "coin": "alps",
                "coinsPerDay": "120.88746224013561914035",
                "diff": 29.5071345,
                "diffAlgo": "1.32313861223036965953",
                "hashAlgo": "0",
                "nethash": 952.87351636221,
                "pos": "?",
                "priceBTC": "0.000001222",
                "priceUSD": "0.01094520959999999848",
                "reward": 65,
                "timestamp": "2018-05-10 20:56:25.949806",
                "type": "diff",
                "usdPerDay": "1.32313861223036965953",
                "value": "1.32313861223036965953",
                "workers": 46
            },
            "order": 64
        }
    },
    "message": "",
    "status": "ok",
    "timestamp": "Thu, 10 May 2018 20:57:06 GMT"
}
  • 2
    A small example would suffice! You likely want key/value pairs from the dictionary's `items()` method. You can pass that into `sorted` which has a key function you use to decide order. Do a bit of searching ,there are many examples out there. – tdelaney May 11 '18 at 05:21
  • Thanks, found some more sorting examples but their JSON data is rather very simple. – Linus TechTips May 11 '18 at 05:33
  • Yours is pretty simple too. If you trim the example down, it'll be easier to see. What do you wanted the sorted data to look like? – tdelaney May 11 '18 at 05:52

2 Answers2

0

If it is possible in this project, I suggest to re-arrange your json data as follows (will be easier to index):

 {
    "data": [{
        "NEWKEY": "aby",
        "info": {
            "algo": "scrypt",
            "bestexchange": 4,
            "bestPool": 11,
            "blocks": 1036871,
            "blocktime": 0,
            "coin": "aby",
            "coinsPerDay": "0.66582227581137665151",
            "diff": 5409.10761,
            "diffAlgo": "0.0058443642207875908",
            "hashAlgo": "0",
            "nethash": 113882.060223,
            "pos": "?",
            "priceBTC": "0.00000098",
            "priceUSD": "0.00877766399999999926",
            "reward": 200.037,
            "timestamp": "2018-05-10 20:56:25.949806",
            "type": "diff",
            "usdPerDay": "0.0058443642207875908",
            "value": "0.0058443642207875908",
            "workers": 311
        },
        "order": 129
    }

more data here

 ]}

Then:

How can I sort a dictionary by key?

Mika72
  • 413
  • 2
  • 12
0

Point of clarification, that data may have come from JSON (which is a serialization format) but its regular python dictionaries at this point. The first step is to shrink the example to see what is going on.

data = {
    "data": {
        "aby": {
            "info": {
                "algo": "scrypt",
                "bestexchange": 4,
                "bestPool": 11,
            },
            "order": 129
        },
        "adz": {
            "info": {
                "algo": "x11",
                "bestexchange": "None",
                "bestPool": 10,
            },
            "order": 138
        },
        "agn": {
            "info": {
                "algo": "neoscrypt",
                "bestexchange": 1,
                "bestPool": 10,
            },
            "order": 53
        },
    },
    "message": "",
    "status": "ok",
    "timestamp": "Thu, 10 May 2018 20:57:06 GMT"
}

"data" is a dict and I think you want to sort its values based on "algo". The values are nested dictionaries and the search key you want is "info" then "algo". Use sorted and supply a key function to extract the object for ordering the sort:

for value in sorted(data["data"].values(), key=lambda valdict: valdict["info"]["algo"]):
    print(value)

Run the code and you get

('agn', {'info': {'bestexchange': 1, 'bestPool': 10, 'algo': 'neoscrypt'}, 'order': 53})
('aby', {'info': {'bestexchange': 4, 'bestPool': 11, 'algo': 'scrypt'}, 'order': 129})
('adz', {'info': {'bestexchange': 'None', 'bestPool': 10, 'algo': 'x11'}, 'order': 138})
tdelaney
  • 73,364
  • 6
  • 83
  • 116