Sorted by the new coming and the existing value

var sortedArr = [];
function getSortedArray(obj) {
  var existIndex = -1;
  sortedArr.forEach(function(e, i) {
    if (e.id === obj.id) {
      existIndex = i;
    }
  });
  if (existIndex >= 0) {
    var a = sortedArr.slice(0, existIndex);
    a.unshift(obj);
    sortedArr = a.concat(sortedArr.slice(existIndex + 1));
  } else {
    sortedArr.unshift(obj);
  }
  return sortedArr;
}
sorted.png

你可能感兴趣的:(Sorted by the new coming and the existing value)