Javascript首先排序重复,然后从小到大排序

时间:2017-02-22 04:28:17

标签: javascript sorting

基本上我希望重复值上升到顶部,然后剩下的值从小到大排序(升序)。

我需要的例子:

246, 246, 246, 100, 216, 553, 600

这是我的代码输出的内容:

100, 216, 246, 246, 246, 553, 600

这是我的代码:

Javascript Demo on JSBin

// Array of objects
var unsorted = [
    {
        id: 1,
        height: 600
    },
    {
        id: 2,
        height: 246
    },
    {
        id: 3,
        height: 216
    },
    {
        id: 4,
        height: 100
    },
    {
        id: 5,
        height: 553
    },
    {
        id: 7,
        height: 246
    },
    {
        id: 6,
        height: 246
    }
];


// Sort by duplicates, then ascending
var sorted = unsorted.sort(function(a, b) {

  // Attempting to sort duplicates first...
    if (a.height === b.height) {
        return -1;
    }

    if (a.height < b.height) {
        return -1;
    }

    if (a.height > b.height) {
        return 1;
    }

    return 0;
}); 

console.log(sorted);

4 个答案:

答案 0 :(得分:1)

使用ES6的sort回调函数,

const sortFunc = (a, b) => {
  const diff = a.height - b.height;
  switch (diff) {
    case 0:
        return -1;
    default:
        return diff;
  }
}

unsortedArr.sort(sortFunc);

答案 1 :(得分:0)

将排序功能更改为 -

var sorted = unsorted.sort(function(a, b) {

    // Attempting to sort duplicates first...
    if (a.height === b.height) {
        return -1;
    }

    if (a.height < b.height) {
        return 0;
    }

    if (a.height > b.height) {
        return 1;
    }

    return 0;
});

console.log(sorted);

答案 2 :(得分:0)

这是一个替代解决方案,您可以使用没有es6样式编写,通过将重复项分组到一个数组中,将不同的值分组到另一个数组中,分别对每个数组进行排序,然后连接两者:

function duplicateSort(objects) {

  // ordering from smallest to largest
  var ascending = function(prev, next) {
    return prev.height - next.height;
  };

  // sorts an arr with an ascending ordering
  var ascendingSort = function(arr) {
    return arr.sort(ascending);
  };

  // checks if item is a duplicate
  var isDuplicate = function(item) {
    return objects.filter(function(obj) {
      return obj.height == item.height
    }).length == 1 ? false : true;
  }

  // checks if item is not a duplicate
  var notDuplicate = function(item) {
    return !isDuplicate(item);
  }

  // partition duplicates vs distinct
  var duplicates = objects.filter(isDuplicate);
  var nonDuplicates = objects.filter(notDuplicate);

  // sort both and flatten/concat into one array
  return [].concat.apply([], [duplicates, nonDuplicates].map(ascendingSort));
}



 var unsorted = [{
    id: 1,
    height: 600
  },
  {
    id: 2,
    height: 246
  },
  {
    id: 3,
    height: 216
  },
  {
    id: 4,
    height: 100
  },
  {
    id: 5,
    height: 553
  },
  {
    id: 6,
    height: 246
  }
];

console.log(duplicateSort(unsorted));

使用sort的解决方案是编写一个比较器来处理duplicate项目,方法是将其移到array的前面并正常排序不同的值(升序)。由于sort一次只检查2个数字,因此将此排序应用于数组的长度。

function duplicateSort2(arr) {

  // checks if element is a duplicate in this array
  var isDuplicate = function(item) {
    return arr.filter(function(obj) {
      return obj.height == item.height
    }).length == 1 ? false : true;
  }

  // custom sort for duplicates
  var ascending = function(prev, next) {
    if (isDuplicate(prev)) return 0;
    if (isDuplicate(next)) return 1;
    return prev.height - next.height;
  }

  // sort on each element
  arr.map(function(item) {
    return arr.sort(ascending);
  });

  return arr;
}

var unsorted = [{id: 1,height: 600},{id: 2,height: 246},{id: 3,height: 216},{id: 4,height: 100},{id: 5,height: 553},{id: 6,height: 246}];

console.log(duplicateSort2(unsorted));

答案 3 :(得分:0)

运行以下代码将为您提供所需的结果。 策略:制作两个数组:唯一成员数组,重复成员数组。对它们进行排序,然后将它们连接在一起。

&#13;
&#13;
// Array of objects
var unsorted = [
    {
        id: 1,
        height: 600
    },
    {
        id: 2,
        height: 246
    },
    {
        id: 3,
        height: 216
    },
    {
        id: 4,
        height: 100
    },
    {
        id: 5,
        height: 553
    },
    {
        id: 7,
        height: 246
    },
    {
        id: 6,
        height: 246
    }
];


var uniqueHeights = [];
var dupHeights = [];
var findIndexByHeight = function(arr, ele) {
  var index = -1;
  arr.forEach(function(innerEle, innerI) {
    if (ele.height === innerEle.height) {
        index = innerI;
    }
  });
  return index;
}

unsorted.forEach(function(ele, i) {
  if(findIndexByHeight(uniqueHeights, ele) === -1) {
    uniqueHeights.push(ele);
  } else  {
    dupHeights.push(ele);
  }
});

for(var idx = 0; idx < uniqueHeights.length; idx++) {
  var dupIdx = findIndexByHeight(dupHeights, uniqueHeights[idx]);
  if(dupIdx !== -1) {
    dupHeights.push(uniqueHeights.splice(dupIdx, 1)[0]);
    idx--;
  }
}
var sortedDup = dupHeights.sort(function(a, b){
  return a.id > b.id;
});
sortedDup = dupHeights.sort(function(a, b){
  return a.height > b.height;
});
var sortedUnique = uniqueHeights.sort(function(a, b){
  return a.height > b.height;
});
var resArr = sortedDup.concat(sortedUnique);

console.log(resArr);
&#13;
&#13;
&#13;

相关问题