Isak La Fleur
Isak La Fleur

Reputation: 4668

How to sort an object by its values with multiple keys?

I want to use the sort method explained here but I have

{
  "CYLINDER": 2986,
  "HYDRAULIC": 1421,
  "JACKS": 84,
  "INSTALLATION": 119,
  "REAR": 61,
  "JACK": 334,
  "TUBE": 1,
  "FEED": 114,
  "ASSEMBLY": 326,
  "DCS": 2,
  "TOWER": 65,
  "RAISING": 8,
  "BREAKOUT": 6,
}

I have created this function where I group every string together and add a count (as the value for the key, see above)

function generateCountDuplication(wordArray) {
  const countedWords = wordArray.reduce(function(allWords, word) {
    if (word in allWords) {
      allWords[word]++;
    }
    else {
      allWords[word] = 1;
    }
    return allWords;
  }, {});
  return countedWords;
}

I have tried to google and look here on Stackoverflow, but have not found any where to sort by the value when the key is not unique. Any clue how to solve?

I want this as a result:

{
  "TUBE": 1,
  "DCS": 2,
  "BREAKOUT": 6,
  "RAISING": 8,
  "REAR": 61,
  "TOWER": 65,
  "JACKS": 84,
  "FEED": 114,
  "INSTALLATION": 119,
  "ASSEMBLY": 326,
  "JACK": 334,
  "HYDRAULIC": 1421,
  "CYLINDER": 2986
}

Upvotes: 0

Views: 100

Answers (5)

mplungjan
mplungjan

Reputation: 177692

Object.entries and a forEach since map is not the most relevant method to copy key-values

and as epascarello mentioned, you are better off with an actual array since you should not rely on key order in an object

const list = {
  "CYLINDER": 2986,
  "HYDRAULIC": 1421,
  "JACKS": 84,
  "INSTALLATION": 119,
  "REAR": 61,
  "JACK": 334,
  "TUBE": 1,
  "FEED": 114,
  "ASSEMBLY": 326,
  "DCS": 2,
  "TOWER": 65,
  "RAISING": 8,
  "BREAKOUT": 6,
},newList = {}

Object.entries(list).sort((a,b) => a[1]-b[1])
  .forEach(elm => newList[elm[0]] = elm[1])

console.log(newList);

Upvotes: 2

Damien
Damien

Reputation: 1600

Unsing Object.keys() and map as follows you can achieve your expected result.

const list = {
  "CYLINDER": 2986,
  "HYDRAULIC": 1421,
  "JACKS": 84,
  "INSTALLATION": 119,
  "REAR": 61,
  "JACK": 334,
  "TUBE": 1,
  "FEED": 114,
  "ASSEMBLY": 326,
  "DCS": 2,
  "TOWER": 65,
  "RAISING": 8,
  "BREAKOUT": 6,
};

var newList = {};
Object.keys(list).sort(function(a,b){return list[a]-list[b]})
                 .map(key => newList[key] = list[key]);
console.log(newList);

Upvotes: 3

Jamiec
Jamiec

Reputation: 136074

Te first thing you'll need to do is turn your objects properties into an array, something in the form

[
   {word: "CYLINDER", count: 2986},
   {word: "HYDRAULIC", count: 4421}
   // etc
]

Then you can use sort.

var input = {
  "CYLINDER": 2986,
  "HYDRAULIC": 1421,
  "JACKS": 84,
  "INSTALLATION": 119,
  "REAR": 61,
  "JACK": 334,
  "TUBE": 1,
  "FEED": 114,
  "ASSEMBLY": 326,
  "DCS": 2,
  "TOWER": 65,
  "RAISING": 8,
  "BREAKOUT": 6,
};

var result = Object.keys(input)
                   .map( x => ({word:x, count:input[x]}))
                   .sort( (a,b) => a.count - b.count);
console.log(result);

Upvotes: 1

Marc
Marc

Reputation: 3876

Combine Object.keys(obj), .sort() & .map

const obj = {
  "CYLINDER": 2986,
  "HYDRAULIC": 1421,
  "JACKS": 84,
  "INSTALLATION": 119,
  "REAR": 61,
  "JACK": 334,
  "TUBE": 1,
  "FEED": 114,
  "ASSEMBLY": 326,
  "DCS": 2,
  "TOWER": 65,
  "RAISING": 8,
  "BREAKOUT": 6,
}

var sorted = {};

console.log("Un-sorted", obj);

Object.keys(obj).sort().map(function(key){
  sorted[key] = obj[key];
});

console.log("Sorted", sorted)

Im sure there is a more elegant solution, but this works too.

Upvotes: 0

gbalduzzi
gbalduzzi

Reputation: 10166

As explained in the doc you linked, the sort function accepts an argument, that is the compareFunction. So, if you want to sort an array of object by their INSTALLATION key, you can simply do:

array.sort(function(a,b) {return a.INSTALLATION - b.INSTALLATION})

Upvotes: 0

Related Questions