Javascript sort custom comparator function – sorting a sorted array

You can have four different comparison functions – one sorting by item1, one by item2, one by item1 then item2 and one by item2 then item1.

E.g.:

arr.sort(function(a,b){
  if(a.item1 == b.item1){
    return a.item2 > b.item2 ? 1 : a.item2 < b.item2 ? -1 : 0;
  }

  return a.item1 > b.item1 ? 1 : -1;
});

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)