This code will help to sort any custom list object in ascending or descending order
accuracy=[
{
"Alg": "DecisionTree",
"val": 90
},
{
"Alg": "GradientBoosting",
"val": 60
},
{
"Alg": "LinearRegression",
"val": 95
},
{
"Alg": "LogisticRegression",
"val": 96
}
]
ascending=sorted(accuracy, key = lambda i: float(i['val']))
print(ascending)
desc=sorted(accuracy, key = lambda i: float(i['val']),reverse=True)
print(desc)
Output
[{'Alg': 'GradientBoosting', 'val': 60}, {'Alg': 'DecisionTree', 'val': 90}, {'Alg': 'LinearRegression', 'val': 95}, {'Alg': 'LogisticRegression', 'val': 96}]
[{'Alg': 'LogisticRegression', 'val': 96}, {'Alg': 'LinearRegression', 'val': 95}, {'Alg': 'DecisionTree', 'val': 90}, {'Alg': 'GradientBoosting', 'val': 60}]