1

I want to manipulate an object {a: 1, b: 20, c: 3, d: 4, e: 1, f: 4} into this {a: 1, e: 1, c: 3, d: 4, f: 4, b:20} (sorting the object numerically in ascending order) in these specific steps (I am aware that there must be easier, more advanced ways):

  1. Defining a function called 'sort' which takes an object as argument and inside it declaring an empty array called 'arr'.

  2. Looping inside the object and for each iteration push an array which contains the key and the value to 'arr'.

  3. Sorting the array numerically by the object value (position one in our nested arrays)

  4. Creating an empty object called 'newObj'.

  5. Looping through the array and for each iteration assign the first and second element as the key and value respectively to our 'newobJ'

  6. Returning the 'newObj'.

This is what I have so far. The code has hickups at the sorting parts but maybe also other things I am not yet aware of.

function sort(object) {
    var arr = []
    var newObj = {}

    Object.entries(object).forEach(function(element, index) {
        arr.push(element)
    })

    arr[index][1].sort(function(a, b){return b-a});  

    arr.forEach(function([key, value], index) {
        newObj[key] = value
    })
    return newObj
}

How do I need to improve my code to make the function work?

  • The keys of an object do not have any explicit order. You'll need an array such as `[{key:'a', value:1},{key:'e',value:1}]` etc – Jamiec Sep 12 '19 at 08:21
  • @Jamiec That is not the case in most environments - property order *is* preserved for most keys in ES2015+ – CertainPerformance Sep 12 '19 at 08:22
  • OK, got the answer but need help with one part of my code: ```arr.sort(function(a, b) { return a[1] - b[1]; });``` Where is a & b coming from? what numbers are compared here? I figured, that the [1] is to take the second value of each nested array, but what are the variables a&b? – Stephan - the curious Sep 12 '19 at 08:37

1 Answers1

3

You could take the entries, sort by the second index and get a new object.

var object = { a: 1, b: 20, c: 3, d: 4, e: 1, f: 4 },
    result = Object.fromEntries(
        Object
            .entries(object)
            .sort((a, b) => a[1] - b[1])
    );

console.log(result);

By taking your algorithm,

arr[index][1].sort(function(a, b){return b-a});  

you need to sort the complete array by

arr.sort(function (a, b) { return a[0] - b[0]; });

Array#sort takes a callback and this has two parameters, one for each item and returns a value which reflects the wanted order of the two elements.

In this case, by taking an array of entries, you have for example two arrays as parameter, like

['a', 1]
['b', 20]

from this arrays take the value from index 1 and return the delta of it.

Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • 1
    I have the feeling that he must follow his requirements: `in these specific steps`, even though a much shorter solution is possible `I am aware that there must be easier, more advanced ways` – CertainPerformance Sep 12 '19 at 08:26
  • @Nina, i actually can take the missing part from your code (bit modified) for my specific setup. Like this: ```arr.sort(function(a, b) { return a[1] - b[1]; });``` Where is a & b coming from? what numbers are compared here? – Stephan - the curious Sep 12 '19 at 08:36
  • @Nina thx ... when you write " ... and this has two parameter, one for each item ..." I do not understand which item you refer to. The original object has 6 items (key/value pairs). The created array has those 6 nested arrays. Does this function always take two items (=nested arrays) and compares each according to the first index (hence a[1] and so on). – Stephan - the curious Sep 12 '19 at 08:47
  • the compare function of `sort` has only two paramters and they are for two elements of the array for sorting. even if an element is an array. (please have a look to the link.) – Nina Scholz Sep 12 '19 at 08:53