-1

How would I go about sorting a json into an array by numeric value and then how would I easily access that information.

{"362439239671087109":{"coins":19},"178538363954003968":{"coins":18},"234255082345070592":{"coins":137}} 

The weird numbers are discord user ids.

3 Answers3

0

Ummm. First, make associative array, if the json is generated from bakcend, by example in php, you can use functions for order arrays. ---> http://php.net/manual/en/array.sorting.php See this: Converting JavaScript object with numeric keys into array

bashman
  • 138
  • 2
  • 11
0
var a = {"362439239671087109":{"coins":19},"178538363954003968":{"coins":18},"234255082345070592":{"coins":137}};
var b = Object.keys(a);
b.sort(function(x,y){return a[x].coins-a[y].coins});
console.log(b);
SQL Hacks
  • 1,322
  • 1
  • 10
  • 15
0

// Code that can handle VERY large numbers by treating them as strings.


var a = {
  "362439239671087109": {
    "coins": 19
  },
  "178538363954003968": {
    "coins": 18
  },
  "234255082345070592": {
    "coins": 137
  }
};


function padWithZeros(s) {
  return ("000000000000000000000" + s).substr(-20);
}

var b = Object.keys(a);
b.sort(
  function(x, y) {
    var s1 = padWithZeros(a[x].coins);
    var s2 = padWithZeros(a[y].coins);

    if (s1 === s2) {
      return 0;
    }

    if (s1 > s2) {
      return 1;
    } else {
      return -1;
    }


  });
console.log(b);
Jeremy J Starcher
  • 23,369
  • 6
  • 54
  • 74