javascript
var sortDict = function (dict, compare) {
let keys = Object.keys(dict)
let sortedKeys = keys.sort(compare)
result = []
for (let i in sortedKeys) {
let aKey = sortedKeys[i]
let anItem = {}
anItem[aKey] = dict[aKey]
result.push(anItem)
}
return result
}
var a = {"zabc": 8, "abc": 6, "jabc": 3, 'baaa': '5'}
console.log(JSON.stringify(sortDict(a, function (k1, k2) {
// return a[k1] > a[k2]
return k1 > k2
})))
You will see the sorted result printed.
No comments:
Post a Comment