How to sort JSON data in JavaScript.

Sorting JSON data in JavaScript can be achieved using the sort() method. This method allows you to sort an array of JSON objects based on a specific property value.

Here’s an example of how you can sort JSON data in JavaScript:

const jsonData = [
  { name: 'John', age: 25 },
  { name: 'Alice', age: 30 },
  { name: 'Bob', age: 20 }
];

// Sort JSON data by age in ascending order
jsonData.sort((a, b) => a.age - b.age);

console.log(jsonData);

In the above example, we have an array of JSON objects called jsonData with properties name and age. We can use the sort() method on this array and provide a comparison function as a parameter.

The comparison function (a, b) => a.age - b.age compares the age property of the objects a and b. The sort() method then rearranges the objects based on the comparison result in ascending order.

The output will be:

[
  { name: 'Bob', age: 20 },
  { name: 'John', age: 25 },
  { name: 'Alice', age: 30 }
]

Likewise, you can sort the JSON data in descending order by reversing the comparison in the comparison function:

// Sort JSON data by age in descending order
jsonData.sort((a, b) => b.age - a.age);

console.log(jsonData);

The output will be:

[
  { name: 'Alice', age: 30 },
  { name: 'John', age: 25 },
  { name: 'Bob', age: 20 }
]

That’s it! Now you know how to sort JSON data in JavaScript using the sort() method. You can customize the sorting logic based on your requirements and the properties of your JSON objects.

#javascript #json #sorting