What is the best way to change the order of array of objects in javascript

What is the easiest way to re-order an array of objects as below.

I want to re-order it based on group value (custom order)

[ { groupValue: 'Corp',
    doclist: { numFound: 259, start: 0, maxScore: 1.1320078, docs: [Object] } },
  { groupValue: 'Phone',
    doclist: { numFound: 7975, start: 0, maxScore: 1.2560269, docs: [Object] } },
  { groupValue: 'Shop',
    doclist: { numFound: 9, start: 0, maxScore: 1.2556685, docs: [Object] } } ]

If I pass in argument with the group value, that particular object corresponding to the group value should come up on the top

Something like

function ("Shop",originalArray){
 return newArray;
}

Should return:

    [ { groupValue: 'Shop',
        doclist: { numFound: 9, start: 0, maxScore: 1.2556685, docs: [Object] } },
{ groupValue: 'Corp',
        doclist: { numFound: 259, start: 0, maxScore: 1.1320078, docs: [Object] } },
      { groupValue: 'Phone',
        doclist: { numFound: 7975, start: 0, maxScore: 1.2560269, docs: [Object] } }]

76 thoughts on “What is the best way to change the order of array of objects in javascript”