How to sort array of object using object keys in JavaScript?
Tags: javascript, sort, object, key
Published: 2021-03-25 11:38:02

Here we are going to sort an array of objects using keys that are available in that objects.

For running our javascript code, we use Nodejs. you can check by typing in terminal or cmd that you have Nodejs in your system or not `node -v`. it will show the version of the node, you have. But if you do not have then you can download it from [nodejs.org](https://nodejs.org/en/).

After that create one file named **test.js**
and we take our dataset
```js
const data = [
  { name: 'madan', age: '12' },
  { name: 'man', age: '13' },
  { name: 'dan', age: '14' },
  { name: 'dam', age: '11' },
  { name: 'ram', age: '17' },
]
```

Now our data is ready, let's start implementing our logic to sort data. for that, we are going to create a function in which we pass two parameters one is `data` and another one is `key`. here `data` define the above dataset and `key` define by which key, you want to sort, available in the object like we have just `name` and `age`.

```js
function sortDataBy (data, byKey){
   //here your logic code
}
```

Now inside the function, we create one variable to store our sorted data.
```js
let sortedData;
```

after that, we are going to check each and every key present in the object with the given key in the variable called `byKey`.
```js
if(byKey == 'name'){
    //your logic for name
}
else if(byKey == 'age'){
    //your logic for age
}
```

first we are writing logic for `name` key

```js
// we use javascript sort function to compare to value
sortedData = data.sort(function(a,b){
   // here a , b is whole object, you can access its property
   //convert both to lowercase
      let x = a.name.toLowerCase();
      let y = b.name.toLowerCase();

   //compare the word which is comes first
      if(x>y){return 1;} 
      if(x<y){return -1;}
      return 0;
    });
```

now we are writing logic for `age` key

```js
sortedData = data.sort(function(a,b){
    // here a , b is whole object, you can access its property

    // it will return the difference to sort function and then 
    // sort compare that difference is equal to 0 or smaller than 0 or 
    // greater than 0. on the basis of that it will give sorted number list
      return a.age - b.age;
    })
```

at the end, just return our data variable `sortedData`
```js
return sortedData;
```

after completing our function, we can call it with different keys of the object to check our output.
```js
console.log('sort by age\n');
console.log(sortDataBy(data, 'name'));
console.log('sort by age\n');
console.log(sortDataBy(data, 'age'));
```

now you can run your file by typing in terminal `node test`
output will be
```shell
sort by age

[
    { name: 'dam', age: '11' },
    { name: 'dan', age: '14' },
    { name: 'madan', age: '12' },
    { name: 'man', age: '13' },
    { name: 'ram', age: '17' }
]
sort by age

[
    { name: 'dam', age: '11' },
    { name: 'madan', age: '12' },
    { name: 'man', age: '13' },
    { name: 'dan', age: '14' },
    { name: 'ram', age: '17' }
]
```

Last note, for easy to use, I give you the full code here.
```js
const data = [
  { name: 'madan', age: '12' },
  { name: 'man', age: '13' },
  { name: 'dan', age: '14' },
  { name: 'dam', age: '11' },
  { name: 'ram', age: '17' },
];

function sortDataBy (data, byKey){
  let sortedData;
  if(byKey == 'name'){
    sortedData = data.sort(function(a,b){
      let x = a.name.toLowerCase();
      let y = b.name.toLowerCase();
      if(x>y){return 1;}
      if(x<y){return -1;}
      return 0;
    });
  }else{
    sortedData = data.sort(function(a,b){
      return a.age - b.age;
    })
  }
  return sortedData;
}

console.log('sort by age\n');
console.log(sortDataBy(data, 'name'));
console.log('sort by age\n');
console.log(sortDataBy(data, 'age'));

```

Thank you, Happy coding!!