-4

I have am array of object i.e. JSON like this in my react state and before rendering it I want it to sort based on status property example- Actual Value

{
"list": [
{
   "id": "aac",
   "name": "James",
   "status": "active"
},
{
   "id": "gds",
   "name": "Alice",
   "status": "left"
},
{
   "id": "cvs",
   "name": "Kristy",
   "status": "active"
},
{
   "id": "cgt",
   "name": "Samaira",
   "status": "left"
},
]
}

Expecting Value after sorting based on status element

{
"list": [
{
   "id": "aac",
   "name": "James",
   "status": "active"
},
{
   "id": "cvs",
   "name": "Kristy",
   "status": "active"
},

{
   "id": "gds",
   "name": "Alice",
   "status": "left"
},

{
   "id": "cgt",
   "name": "Samaira",
   "status": "left"
},
]
}

Neha Sharma
  • 461
  • 2
  • 7
  • 21
  • 3
    What have you tried, and what exactly is the problem with it? – jonrsharpe Sep 02 '20 at 19:17
  • 1
    If you need to **sort** an **array** in **javascript** I suggest you google "sort array javascript" to read the documentation on Array.sort at MDN which is the first result : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort – Sheraff Sep 02 '20 at 19:20
  • 1
    Does this answer your question? [Sorting an array of objects by property values](https://stackoverflow.com/questions/979256/sorting-an-array-of-objects-by-property-values) – Emile Bergeron Sep 02 '20 at 20:03

1 Answers1

3

This will sort alphabetically by status

arrayName.sort((objA, objB) => objA.status > objB.status ? 1 : -1)