0

Edit: This seems related to the way the chrome console handles object evaluation as noted by
jsN00b

original here

I'm sorting an array by name and then by age, logging the array three times: Once right after initialization, second and third time is after sorting the array by name and then age respectively.

The first console.log() call however, prints the array in its sorted state, but what I expected to have is the array in it's unsorted state.

Not sure if this is related to the way JavaScript handles array in memory or is is it a bug?

Code:

function byField(field) {
  return (objA, objB) => (objA[field] > objB[field] ? 1 : -1);
}

let users = [
  { name: "John", age: 20, surname: "Johnson" },
  { name: "Pete", age: 18, surname: "Peterson" },
  { name: "Ann", age: 19, surname: "Hathaway" },
];

console.log(users);
users.sort(byField("name"));
console.log(users);
users.sort(byField("age"));
console.log(users);
.as-console-wrapper { max-height: 100% !important; top: 0 }

Expected output:

//unsorted
[
  { name: "John", age: 20, surname: "Johnson" },
  { name: "Pete", age: 18, surname: "Peterson" },
  { name: "Ann", age: 19, surname: "Hathaway" },
];

//by name
[
  { name: "Ann", age: 19, surname: "Hathaway" },
  { name: "John", age: 20, surname: "Johnson" },
  { name: "Pete", age: 18, surname: "Peterson" },
];

//by age
[
  { name: "Pete", age: 18, surname: "Peterson" },
  { name: "Ann", age: 19, surname: "Hathaway" },
  { name: "John", age: 20, surname: "Johnson" },
];

Actual Output:

//unsorted
[
  { name: "Pete", age: 18, surname: "Peterson" },
  { name: "Ann", age: 19, surname: "Hathaway" },
  { name: "John", age: 20, surname: "Johnson" },
];

//by name
[
  { name: "Pete", age: 18, surname: "Peterson" },
  { name: "Ann", age: 19, surname: "Hathaway" },
  { name: "John", age: 20, surname: "Johnson" },
];

//by age
[
  { name: "Pete", age: 18, surname: "Peterson" },
  { name: "Ann", age: 19, surname: "Hathaway" },
  { name: "John", age: 20, surname: "Johnson" },
];
Elbasel
  • 11
  • 4
  • I've placed the original code into a snippet. When executing as a snippet the order of the first console remains exactly how the array is populated - which is expected. Arrays are expected to retain the order. So, if we set an array as :`[1, 3, 2]`, unless it is changed, the order will be retained (per my admittedly noob-ish understanding). – jsN00b Jun 01 '22 at 10:52
  • function byField(field) { return (objA, objB) => (objA[field] > objB[field] ? 1 : -1); } let users = [ { name: "John", age: 20, surname: "Johnson" }, { name: "Pete", age: 18, surname: "Peterson" }, { name: "Ann", age: 19, surname: "Hathaway" }, ]; const sortedusers = users.sort(byField("name")); console.log(sortedusers); //if you want to age sort use this code const sortedusersage = users.sort(byField("age")); console.log(sortedusersage); – Hazrat Gafulov Jun 01 '22 at 11:11

1 Answers1

0

Method Array.prototype.sort does mutate your initial array. For fix this problem you need to create copy of array and then sort this copy. It is like follow example:

[...users].sort(byField("name"));
an_parubets
  • 589
  • 7
  • 15