Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort an array of objects if property exists

I have an array of objects. Out of each of these objects onle few of them have a property and I want to sort the array putting those objects at top which have that property.

Ex -

arr= [{key1: "obj1val1", key2 :"obj1val2"}, 
      {key2 :"obj2val2"}, 
      {key1: "obj3val3", key2 :"obj3val3"},
      {key2 :"obj4val1"},
      {key1: "obj5val1", key2 :"obj5val2"}
]

Sorted by key1

Expected Result - [{key1: "obj1val1", key2 :"obj1val2"}, 
                   {key1: "obj3val3", key2 :"obj3val3"},
                   {key1: "obj5val1", key2 :"obj5val2"}, 
                   {key2 :"obj2val2"}, 
                   {key2 :"obj4val1"}]

I have tried below approach-

sort : function(arr) {
              var copyArr = arr.slice();
              var newArr = [];
              for(var i=0; i<arr.length; i++){
                if(arr[i].key1){
                  newArr.push(arr[i]);
                    copyArr.splice(i, 1);
                }
              }
              Array.prototype.push.apply(newArr, copyArr);
              return newArr;
            }

Not exactly but I see discrepancy from the above approach. For a array bigger length, some of the objects which have key1 are not moved to top. Can this be done by internal sort method of javascript? If yes, how will the custom function compare ?

Can only support ES5 methods.

like image 775
RahulB Avatar asked Dec 24 '22 04:12

RahulB


2 Answers

Can this be done by internal sort method of javascript

Yes: Simply prefer the entry that has the property:

arr.sort(function(left, right) {
    return left.hasOwnProperty("key1") ? -1 : right.hasOwnProperty("key1") ? 1 : 0
});

Live Example:

var arr = [
    {key1: "obj1val1", key2 :"obj1val2"}, 
    {key2 :"obj2val2"}, 
    {key1: "obj3val3", key2 :"obj3val3"},
    {key2 :"obj4val1"},
    {key1: "obj5val1", key2 :"obj5val2"}
];
arr.sort(function(left, right) {
    return left.hasOwnProperty("key1") ? -1 : right.hasOwnProperty("key1") ? 1 : 0
});
console.log(arr);

If you also want to sort by that property's value:

arr.sort(function(left, right) {
    var leftHas = left.hasOwnProperty("key1");
    var rightHas = right.hasOwnProperty("key1");
    if (leftHas && rightHas) {
      return left.key1.localeCompare(right.key1);
    }
    return leftHas ? -1 : rightHas ? 1 : 0;
});

Live Example:

var arr = [
    {key1: "obj1val1", key2 :"obj1val2"}, 
    {key2 :"obj2val2"}, 
    {key1: "obj3val3", key2 :"obj3val3"},
    {key2 :"obj4val1"},
    {key1: "obj5val1", key2 :"obj5val2"}
];
arr.sort(function(left, right) {
    var leftHas = left.hasOwnProperty("key1");
    var rightHas = right.hasOwnProperty("key1");
    if (leftHas && rightHas) {
      return left.key1.localeCompare(right.key1);
    }
    return leftHas ? -1 : rightHas ? 1 : 0;
});
console.log(arr);
like image 143
T.J. Crowder Avatar answered Jan 08 '23 03:01

T.J. Crowder


Here is a working snippet that gives your requested result and is easily extended. It uses the native array sort.

arr = [{
    key1: "obj1val1",
    key2: "obj1val2"
  },
  {
    key2: "obj2val2"
  },
  {
    key1: "obj3val3",
    key2: "obj3val3"
  },
  {
    key2: "obj4val1"
  },
  {
    key1: "obj5val1",
    key2: "obj5val2"
  }
]

const sorted = arr.sort((a, b) => {
  const k1 = a.key1 === undefined ? 0 : 1
  const k2 = b.key1 === undefined ? 0 : 2
  return k2- k1
})
console.log(sorted)
like image 38
Steven Spungin Avatar answered Jan 08 '23 04:01

Steven Spungin