0

I'm writing a sort function that takes array and key as parameters and returns the sorted array of objects within the array by the key specified in the parameter

So far, I've tried to use the compare function using sort to alphabetize an object -- however it gets tricky when I try to write a function that takes any array of objects and sorts them by any key

solarSystem =[
    {name: "Mercury", position: 1},
    {name: "Venus", position: 2},
    {name: "Earth", position: 3},
    {name: "Mars", position: 4},
    {name: "Jupiter", position: 5},
    {name: "Saturn", position: 6},
    {name: "Uranus", position: 7},
    {name: "Neptune", position: 8},
    {name: "Pluto", position: 9}

];

sortArrayOfObjects = (array, key) => {
    array.sort(function (a, b){
        let key1 = array.key.toUpperCase();
        let key2 = array.key.toUpperCase();
        if (key1 < key2){
            return -1;
        } else if (key1 > key2){
            return 1;
        }
        return 0;
    });
}

console.log(sortArrayOfObjects(solarSystem, "name"));

I expect the output to be:

[
  { name: 'Earth', position: 3 },
  { name: 'Jupiter', position: 5 },
  { name: 'Mars', position: 4 },
  { name: 'Mercury', position: 1 },
  { name: 'Neptune', position: 8 },
  { name: 'Pluto', position: 9 },
  { name: 'Saturn', position: 6 },
  { name: 'Uranus', position: 7 },
  { name: 'Venus', position: 2 }
]

but instead it indicates that key is undefined

  • you need to take the bracket accessor with the key, like `let key1 = a[key].toUpperCase();`. you may better take another name than `key1` it is the value, you are getting and you need this for sorting. `a` and `b` are items of the array, not indices. – Nina Scholz Jul 20 '19 at 17:13

2 Answers2

0

Write custom sort callback function to sort. Reference

var solarSystem =[
   {name: "Mercury", position: 1},
   {name: "Venus", position: 2},
   {name: "Earth", position: 3},
   {name: "Mars", position: 4},
   {name: "Jupiter", position: 5},
   {name: "Saturn", position: 6},
   {name: "Uranus", position: 7},
   {name: "Neptune", position: 8},
   {name: "Pluto", position: 9}
];

solarSystem.sort(function(a, b) {
   return a.name > b.name ? 1 : -1;
});
    
console.log(solarSystem)

Alternatively, with the help of ES6 arrow function:

let solarSystem =[
    {name: "Mercury", position: 1},
    {name: "Venus", position: 2},
    {name: "Earth", position: 3},
    {name: "Mars", position: 4},
    {name: "Jupiter", position: 5},
    {name: "Saturn", position: 6},
    {name: "Uranus", position: 7},
    {name: "Neptune", position: 8},
    {name: "Pluto", position: 9}
];

solarSystem.sort((a, b) => (a.name > b.name) ? 1 : -1)
console.log(solarSystem)
MH2K9
  • 11,951
  • 7
  • 32
  • 49
0

Try this:

solarSystem =[
    {name: "Mercury", position: 1},
    {name: "Venus", position: 2},
    {name: "Earth", position: 3},
    {name: "Mars", position: 4},
    {name: "Jupiter", position: 5},
    {name: "Saturn", position: 6},
    {name: "Uranus", position: 7},
    {name: "Neptune", position: 8},
    {name: "Pluto", position: 9}

];

sortArrayOfObjects = (array, key) => {
    array.sort(function (a, b){
        let key1 = a[key].toUpperCase();
        let key2 = b[key].toUpperCase();
        if (key1 < key2){
            return -1;
        } else if (key1 > key2){
            return 1;
        }
        return 0;
    });
    return array
}

console.log(sortArrayOfObjects(solarSystem, "name"));
Saurabh Agrawal
  • 7,581
  • 2
  • 27
  • 51
  • This works except it leaves the 'key' parameter unread and defines the key 'name' inside the function. I'm trying to write a function that takes any object and sorts it by any key. So if i wanted to use the same function to sort a different array of objects I could just plug in the array and key. Does that make sense? – Collin Smith Jul 21 '19 at 15:51
  • AWESOME! Really appreciate your help. – Collin Smith Jul 21 '19 at 16:28