0
let tags = {
    "01": {
       contentID: [10, 20],
       occurrences: 1
    },
    "02": {
       contentID: [10, 20],
       occurrences: 1
    },
    "04": {
       contentID: [10, 20],
       occurrences: 3
    },
    "05": {
       contentID: [10, 20],
       occurrences: 6
    }
};

How to sort this in descending order by taking occurrences as base, This is a global variable. So I want to store it in a different object and sort that one with descending order..

ggorantala
  • 196
  • 2
  • 16
  • `tags` is an object, and objects in Javascript are not ordered to my knowledge. Your basic task at hand is to turn this information from an object, into an array, and then use some variety of sorting algorithm to sort your newly created array. I'm unsure if the keys you have (`01`, `02`, etc.) are necessary data, but if they are you would need to determine a way to keep track of those through the sorting process – Jhecht Oct 11 '18 at 19:46

3 Answers3

2

You can not sort objects, but you can transform it to array of values:

Object.values(tags).sort((a, b) => b.occurrences - a.occurrences)

The result will be:

[
  {
    contentID: [10, 20],
    occurrences: 6
  },
  {
    contentID: [10, 20],
    occurrences: 3
  },
  {
    contentID: [10, 20],
    occurrences: 1
  },
  {
    contentID: [10, 20],
    occurrences: 1
  }
]

However you can sort your keys separately if you need them as well:

Object.keys(tags).sort((a, b) => tags[b].occurrences - tags[a].occurrences)

And your result will be an array ['05', '04', '02', '01']

Eugene Tsakh
  • 2,777
  • 2
  • 14
  • 27
1

Kindly note that objects in javascript dont have any sense of order.

However you can create and array to hold your result.

To get it in descending order ,you can do

Object.values(tags).sort((a,b) => b.occurrences - a.occurrences)

ashish singh
  • 6,526
  • 2
  • 15
  • 35
  • _Note_ that this will use a bubble sort, which is among the worst sorting algorithms. If OP needs a faster sort, then using QuickSort, Radix Sort, Bucket, or Insertion Sort can save quite a bit of time. – Jhecht Oct 11 '18 at 19:48
  • @Jhecht: Not as far as I know. The sort algorithm is not specified by the spec, but I think all current JS engines use efficient sorts. – Scott Sauyet Oct 11 '18 at 20:31
  • You are correct, I had misunderstood what I had read. These sorts uses anywhere between a merge sort to possibly a radix sort depending on the array setup. – Jhecht Oct 11 '18 at 21:03
1

Other suggestions to think about this as an array are probably sound. It's not a good idea to think of Javascript objects as ordered, even though there is a key-ordering specification. But, if you want to keep the results in the same format, you can do so, and still end up sorting the keys:

let tags = {"01": {"contentID": [10, 20], "occurrences": 1}, "02": {"contentID": [10, 20], "occurrences": 1}, "04": {"contentID": [10, 20], "occurrences": 3}, "05": {"contentID": [10, 20], "occurrences": 6}}

const sorted = Object.entries(tags).sort(([, {occurrences: a}], [, {occurrences: b}]) => b - a)
  .reduce((a, [k, v]) => Object.assign(a, {[k]: v}), {})

console.log(sorted)

I can't see what this is really good for, though. The only place you can see a difference in behavior is if you use something like Object.keys, Object.values, or Object.entries on it, again creating arrays.

Scott Sauyet
  • 49,207
  • 4
  • 49
  • 103
  • this approach looks good, but the sorting is not working.. it is displaying the same order as original tags object – ggorantala Oct 12 '18 at 11:09
  • your method didn't sort the object, can you let me know how to sort it ? This is what I want to achieve and not as object.values(..) which gives me an array, as I am using Mustache to render the content at UI. – ggorantala Oct 12 '18 at 12:24
  • 1
    It's quite possible that the sorting is happening, but that your console reorders it for display. You could double-check by calling `Object.keys` on the results. Note that the specifications regarding this are quite complex, and consoles are free to do as they choose. There is some good information in [2ality.com](http://2ality.com/2015/10/property-traversal-order-es6.html). Again, arrays are probably your best bet for anything sorted. – Scott Sauyet Oct 12 '18 at 14:28