-2

I have an array with a single object populated like so:

valueArr = [{
  485: 201.5897, 
  487: 698.52,
  598: 351.85, 
  ...
  year: '2016'
}];

Now, i want to rearange / sort the object from largest - smallest value. The Ouptput i'm looking for would be something like this:

valueArr = [{
  487: 698.52,
  598: 351.85, 
  485: 201.5897, 
  ...
  year: '2016'
}];

NOTE: The "year"-property should be excluded in the sorting. There is only one property of this inside the object.

Is it possible to rearange/sort an object like this?

4 Answers4

1

In theory, properties have no order, but in reality, object properties follow their insertion order due to the creation of hidden classes for each object (see V8 description for a specific example).

Therefore, you can sort your keys:

function sortPropertiesByValue(object) {
  const keys = Object.keys(object);
  const valuesIndex = keys.map(key => ({ key, value: object[key] }));

  valuesIndex.sort((a,b) => b.value - a.value); // reverse sort

  const newObject = {};

  for (const item of valuesIndex) {
    newObject[item.key] = item.value;
  }

  return newObject;
}

Remember that this is only valid on some JS engines, such as V8, since properties order is not part of the spec. You should prefer an array, or a Map if you need key/value pairs.

enter image description here

Aurelien Ribon
  • 7,548
  • 3
  • 43
  • 54
1

Object properties order can not be guaranteed, but you could control the order on how to access the Object properties.

Here is an example.

var valueArr = [{
  485: 201.5897, 
  487: 698.52,
  598: 351.85, 
  year: '2016'
}];

var tmp = {};

var sortedByVal = Object.keys(valueArr[0])
.filter( x => /^\d/g.test(x))
.map(function(x) {
  tmp[this[x]] = x;
  return this[x]; 
},valueArr[0])
.sort((a,b) => b - a);

sortedByVal.forEach(x => console.log(tmp[x], ':', x))
DavidDomain
  • 14,976
  • 4
  • 42
  • 50
0

I like to take the object keys out of the object, store them in an array. Then I use the array to sort my keys how I like them, and create a temporary new object where I get my properties in order, and return that temporary object back to the original object.

valueArr = sortObject(valueArr)

function sortObject(parObj){
  var keys = [];
  for(var key in parObj) { keys.push(key) }
  keys.sort() //or how you choose to sort the numbers
  var tempObj = {};
  for(var i = 0; i < keys.length; i++){ tempObj[keys[i]] = parObj[keys[i]] }
  return tempObj
}
NachoDawg
  • 1,533
  • 11
  • 21
0

Are you asking how to get the source code sorted for you? Like this?

487: 698.52,
598: 351.85,
485: 201.5897,
year: 2016

If that's the case, this script will print out the attribute names and values in a sorted manner:

function sortAndPrintMagic(opsObject) {
    var sortedArray = []
    for (var i1 in opsObject) {

        // Ignore the 'year' attribute
        if (i1 == 'year')
            continue;

        var i3 = 0;
        for (var i2 in sortedArray) {
            if (opsObject[i1] > opsObject[sortedArray[i2]])
                break;
            else
                i3++;
        }

        sortedArray.splice(i3, 0, i1);

    }

    var result = ""
    for (var i in sortedArray)
        result += sortedArray[i] + ": " + opsObject[sortedArray[i]] + ",\n";
    result += "year: " + opsObject.year;
    console.log(result);
}

valueArr = {
  485: 201.5897, 
  487: 698.52,
  598: 351.85, 
  year: '2016'
};

sortAndPrintMagic(valueArr)
birgersp
  • 3,909
  • 8
  • 39
  • 79