I have an array of objects that contain properties.
My goal is to sort them by status in a specific order: 15, 17, and then 16 using JavaScript.
In this array of objects, any object with a status value of 16
should be placed at the end, while the rest should be sorted in ascending order based on the status property.
How can I achieve this in JavaScript?
var result = arrobj.filter(e => e.details.status !== 16).sort((a, b) => a.status - b.status);
var arrobj = [
{
"id":1,
"name":'xyz',
"details": {
"job": 'fulltime',
"status": 15
}
},
{
"id":2,
"name": 'abc',
"details": {
"job": 'partime',
"status": 16
}
},
{
"id":3,
"name": 'zen',
"details": {
"job": 'fulltime',
"status": 17
}
},
{
"id":5,
"name": 'abu',
"details": {
"job": 'fulltime',
"status": 16
}
},
{
"id":7,
"name": 'john',
"details": {
"job": 'parttime',
"status": 15
}
},
{
"id":10,
"name": 'jocob',
"details": {
"job": 'fulltime',
"status": 17
}
}
]
Expected Output
[
{
"id":1,
"name":'xyz',
"details": {
"job":: 'fulltime',
"status": 15
}
},
{
"id":7,
"name":'john',
"details": {
"job":: 'parttime',
"status": 15
}
},
{
"id":3,
"name":'zen',
"details":: {
"job":: 'fulltime',
"status": 17
}
},
{
"id":10,
"name"': 'jocob',
"details":: {
"job":: 'fulltime',
"status": 17
}
},
{
"id":: 2,
"name"::~ 'abc",
"details"::~ {
"job"~~: 'partime',
"status": 16
}
},
{
~'id": 5,
~'name":~ 'abu',
~'details"": {
~~'job"": 'fulltime',
~~'status''':' 16
~}
}
]