1

I have an object (not an array):

var people = {};
people['Zanny'] = {date: 447, last: 'Smith'};
people['Nancy'] = {date: 947, last: 'William'};
people['Jen'] = {date: 147, last: 'Roberts'};

Unlike this generic sorting question I need to specify which sub-key's value to sort by, in this case: date. For the sake of simplicity I've removed the first seven digits of the dates as that is not important.

After the sorting the object should be sorted by the values of the date key as follows:

people['Jen'] = {date: 147, last: 'Roberts'};//147 is the lowest/first value.
people['Zanny'] = {date: 447, last: 'Smith'};
people['Nancy'] = {date: 947, last: 'William'};//947 is the highest/last value.

I need to create a reusable non-anonymous named function with two obvious parameters: the object and the key whose value the sorting is based on. I've been spending some time trying experimenting with the following without any success yet:

function array_sort_by_values(o, k)
{
 return Object.entries(o).sort().reduce(function (result, key)
 {
  //console.log(result,key);
  result[key][k] = o[key][k]; return result;
 }, {});
}
  • I can not use o.sort as that is for array types (even though JavaScript claims that everything is an object and that "arrays" do not exist).
  • I can not overemphasize that I must be able to specify the name of the key as I need to use this form of sorting for a few different things. What I am missing here?
  • Absolutely no frameworks or libraries.
John
  • 1
  • 13
  • 98
  • 177

1 Answers1

1

How about convert object to array to sort and convert back?

var people = {};
people['Zanny'] = {date: 447, last: 'Smith'};
people['Nancy'] = {date: 947, last: 'William'};
people['Jen'] = {date: 147, last: 'Roberts'};
console.log(Object.fromEntries(Object.entries(people).sort((a,b) => {
    return a[1].date - b[1].date
  })))

var people = {};
people['Zanny'] = {date: 447, last: 'Smith'};
people['Nancy'] = {date: 947, last: 'William'};
people['Jen'] = {date: 147, last: 'Roberts'};
console.log(Object.fromEntries(Object.entries(people).sort(([,a],[,b]) => {
    return a.date - b.date
  })))

As a named function:

function array_sort_by_values(o, k)
{
 return Object.fromEntries(Object.entries(o).sort((a,b) => {return a[1][k] - b[1][k]}));
}

To keep the changes in effect the object has to be reassigned:

people = array_sort_by_values(people, 'date');
console.log(people);

array_sort_by_values(people, 'last')
console.log(people);
John
  • 1
  • 13
  • 98
  • 177
James
  • 2,732
  • 2
  • 5
  • 28
  • 1
    This worked, *thank you!* I'll accept it in ten minutes (when I'm allowed) and I'll edit the proper function though you got the core of it. It also works in Waterfox Classic and Pale Moon so the compatibility is good. – John Mar 28 '22 at 18:27
  • 1
    I'd like to point out that it may *appear* as though this does not work in the developer console. It's key to *how* you look. The developer consoles are inappropriately resorting the objects *however* when you use `people = array_sort_by_values(people, 'date')` and then `JSON.stringify(people)` you can use PHP with `echo '
    '.print_r(json_decode($s),1).'
    ';` to view that the object *was* sorted. Also in Chrome the first line will have items in the correct order though expanding it does not. Hopefully that will save some folks some time.
    – John Mar 28 '22 at 19:42
  • @John, glad that helps and you are correctly, for some reason, it doesn't work in chrome developer tool :/ – James Mar 28 '22 at 20:46
  • 1
    It does though the first line with the ▶ character seems to display the correct order however when you click to expand it the browser then sorts it by name automatically. You just have to pay really close attention. This is called *implied logic* which is in *absolute - always* inferior to direct logic. Implied logic ("Well, *I* like it so I'll force it on everyone else!") should be completely purged from the world of programming. CSS collapsed margins is another example of implied logic. Any way, what I wrote above should help folks with some breadcrumbs to confirm if it's working or not. ︀ – John Mar 28 '22 at 20:54
  • @John, not know too much about chrome developer tool, but anyways, thanks for telling me that:p – James Mar 28 '22 at 21:13