-4

I have an object which has some values.

var x = {  
           'A': 4.5,
           'B': 2,
           'C': 4.5,
           'D': 2,
           'E': 2,
           'F': 2,
           'G': 2,
           'H': 4.5,
           'I': 2.5,
           'J': 2.5,
           'K': 2.5,
           'L': 2.5,
           'M': 2.5,
           'N': 2.5,
           'O': 2.5,
           'P': 2.5 
        }

Now I want to sort it with proper key value sequence. The desired result should be like this

{  
               'A': 4.5,
               'H': 4.5,
               'I': 2.5,
               'J': 2.5,
               'K': 2.5,
               'L': 2.5,
               'M': 2.5,
               'N': 2.5,
               'O': 2.5,
               'P': 2.5,
               'B': 2,
               'D': 2,
               'E': 2,
               'F': 2,
               'G': 2,
            }

I tried to do it but I'm not able to sort the result in a sequential manner. my code is

const valueholder = Object.keys(x).map(key => ({ name: key, value: x[key] })).sort((f, s) => s.value - f.value);

By this value is sorted but not in a sequential manner. What I get is this.

[ { name: 'A', value: 4.5 },
  { name: 'C', value: 4.5 },
  { name: 'H', value: 4.5 },
  { name: 'O', value: 2.5 },
  { name: 'N', value: 2.5 },
  { name: 'M', value: 2.5 },
  { name: 'L', value: 2.5 },
  { name: 'P', value: 2.5 },
  { name: 'K', value: 2.5 },
  { name: 'J', value: 2.5 },
  { name: 'I', value: 2.5 },
  { name: 'B', value: 2 },
  { name: 'G', value: 2 },
  { name: 'F', value: 2 },
  { name: 'E', value: 2 },
  { name: 'D', value: 2 } ]

Any help is appreciated.

Iron_Man
  • 161
  • 11
  • 1
    Don't only sort on the value but also on the name – Andreas Dec 27 '17 at 09:28
  • @Andreas OP wants object's values to be sorted instead of turning it into a `Array`. – gurvinder372 Dec 27 '17 at 09:32
  • @gurvinder372 _"By this value is sort **but not coming in a sequential manner**"_ – Andreas Dec 27 '17 at 09:34
  • @Andreas OP has shared the *desired result* as well. – gurvinder372 Dec 27 '17 at 09:35
  • @gurvinder372 1. the properties of an object have no order (other than the one they have been added) 2. I don't see any complaints on the format but only on the sort order. And as the duplicate shows I'm not the only one with this opinion – Andreas Dec 27 '17 at 09:37
  • Best you can do is `valueholder.reduce( ( a, b ) => ( a.set(b.name, b.value), a ) ,new Map());` And use `Map.entries` to iterate the result – gurvinder372 Dec 27 '17 at 09:37

1 Answers1

0

You could sort the keys as well with the same property value.

var x = { A: 4.5, B: 2, C: 4.5, D: 2, E: 2, F: 2, G: 2, H: 4.5, I: 2.5, J: 2.5, K: 2.5, L: 2.5, M: 2.5, N: 2.5, O: 2.5, P: 2.5 },
    order = Object
        .keys(x)
        .sort((a, b) => x[b] - x[a] || a.localeCompare(b));
        //              ^^^^^^^^^^^                        value part
        //                             ^^^^^^^^^^^^^^^^^^  key part

console.log(order);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392