DEV Community

Dezina
Dezina

Posted on

Sorting array without using splice to get the exact output

const getfunction = async ({ query, queryCondition, params }) => {

try {
const candidates = []
const rejectedArray = []
const droppedArray = []
let alldata = await CollectionName.aggregate(
{
$sort: sortQuery
},

])
for (var x=0; x<alldata.length; x++) {

    if(alldata[x].stat != 'Rejected' && alldata[x].stat != 'Dropped') {
      candidates.push(alldata[x])
    }
    else if(alldata[x].stat === 'Dropped') {
      droppedArray.push(alldata[x])
    }
    else if(alldata[x].stat === 'Rejected') {
      rejectedArray.push(alldata[x])
    }   
}

for(var y=0; y<droppedArray.length; y++) {
  candidates.push(droppedArray[y])
}
for(var z=0; z<rejectedArray.length; z++) {
  candidates.push(rejectedArray[z])
}
Enter fullscreen mode Exit fullscreen mode

} //close try
catch (err) {
console.log(err)
}
} //close function

Top comments (0)