如何在 Underscore js - 数组中按(特定顺序)设置顺序

how to set order by (specific order) in Underscore js - Arrays

我有一个数组,我需要基于自定义排序顺序的最终输出 CategoryId:(5,9,14,1,9,3) 基于 TypeID

        console.log(_.sortBy(arr, 'CategoryID')); 

我可以对 CategoryId 进行排序,但我想按特定顺序按 CategoryID 进行排序。

<html> 
<head> 
    <script type="text/javascript" src= 
    "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore.js"> 
    </script> 
</head> 
<body> 
    <script type="text/javascript"> 
        var arr = [ 
            {name: 'kim', salary: 4000,CategoryId:3, TypeId:1}, 
            {name: 'shelly', salary: 5000,CategoryId:14, TypeId:1}, 
            {name: 'don', salary: 100,CategoryId:9, TypeId:1}, 
            {name: 'mark', salary: 4500,CategoryId:5, TypeId:2}, 
            {name: 'mark', salary: 4500,CategoryId:5, TypeId:1}, 
          {name: 'joh', salary: 3450,CategoryId:9, TypeId:1}, 
          {name: 'joe', salary: 5100,CategoryId:1, TypeId:1}, 
          {name: 'moore', salary: 5100,CategoryId:14, TypeId:2}, 
          {name: 'wane', salary: 1500,CategoryId:14, TypeId:2}, 
          {name: 'dick', salary: 400,CategoryId:3, TypeId:1}                
        ]; 
        console.log(_.sortBy(arr, 'CategoryId')); 
    </script> 
</body> 

我希望结果集的特定顺序为
类别 ID:(5,9,14,1,3)

最终输出:

const output = [{
  name: 'mark',
  salary: 4500,
  CategoryId: 5,
  TypeId: 1,
}, {
  name: 'joh',
  salary: 3450,
  CategoryId: 9,
  TypeId: 1,
}, {
  name: 'don',
  salary: 100,
  CategoryId: 9,
  TypeId: 1,
}, {
  name: 'joh',
  salary: 3450,
  CategoryId: 9,
  TypeId: 1,
}, {
  name: 'shelly',
  salary: 5000,
  CategoryId: 14,
  TypeId: 1,
}, {
  name: 'joe',
  salary: 5100,
  CategoryId: 1,
  TypeId: 1,
}, {
  name: 'kim',
  salary: 4000,
  CategoryId: 3,
  TypeId: 1,
}, {
  name: 'mark',
  salary: 4500,
  CategoryId: 5,
  TypeId: 2,
}, {
  name: 'moore',
  salary: 5100,
  CategoryId: 14,
  TypeId: 2,
}, {
  name: 'wane',
  salary: 1500,
  CategoryId: 14,
  TypeId: 2,
}];

为此,您必须制作自己的自定义排序功能,因为使用 underscore.js

无法实现此功能
var tempArray = [
    {name: 'kim', salary: 4000, CategoryId: 3, TypeId: 1},
    {name: 'shelly', salary: 5000, CategoryId: 4, TypeId: 1},
    {name: 'don', salary: 100, CategoryId: 9, TypeId: 1},
    {name: 'mark', salary: 4500, CategoryId: 5, TypeId: 2},
    {name: 'mark', salary: 4500, CategoryId: 5, TypeId: 1},
    {name: 'joh', salary: 3450, CategoryId: 9, TypeId: 1},
    {name: 'joe', salary: 5100, CategoryId: 1, TypeId: 1},
    {name: 'moore', salary: 5100, CategoryId: 14, TypeId: 2},
    {name: 'wane', salary: 1500, CategoryId: 14, TypeId: 2},
    {name: 'dick', salary: 400, CategoryId: 3, TypeId: 1}
]

function sort(order, array) {
    result = []
    order.forEach(o => {
        let output = array.filter(element => element.CategoryId === o)
        // To further sort it by type id we can now use sort
        output = _.sortBy(output, 'TypeId')
        result = result.concat(output)
    })
    // For output to be this way 
    // TypeID: (1) - order by : 5,9,14,1,9,3, TypeID: 2 
    // Order by : 5,9,14,11,9,3.
    // we can use groupBy
    return _.groupBy(result, 'TypeId')
}

sort([5, 9, 14, 1, 9, 3], tempArray)

这是否回答了您的问题?

访问https://underscorejs.org/#objects

sortBy_.sortBy(列表,iteratee,[上下文]) Returns 列表的(稳定)排序副本,按 运行 每个值通过 iteratee 的结果按升序排列。 iteratee 也可以是要排序的 属性 的字符串名称(例如长度)。

<script type="text/javascript">
      var arr = [
         {name: 'kim', salary: 4000,CategoryId:3},
         {name: 'shelly', salary: 5000,CategoryId:4},
         {name: 'don', salary: 100,CategoryId:9},
         {name: 'mark', salary: 4500,CategoryId:5},
         {name: 'mark', salary: 4500,CategoryId:5},
         {name: 'joh', salary: 3450,CategoryId:9},
         {name: 'joe', salary: 5100,CategoryId:1},
         {name: 'moore', salary: 5100,CategoryId:14},
         {name: 'wane', salary: 1500,CategoryId:14},
         {name: 'dick', salary: 400,CategoryId:3}
     ];
     console.log(_.sortBy(arr, 'CategoryId'));
 </script>

如果您将所需顺序指定为数组,则可以根据该数组中每个类别的索引进行排序。

var arr = [ {name: 'kim', salary: 4000,CategoryId:3, TypeId:1}, {name: 'shelly', salary: 5000,CategoryId:14, TypeId:1}, {name: 'don', salary: 100,CategoryId:9, TypeId:1}, {name: 'mark', salary: 4500,CategoryId:5, TypeId:2}, {name: 'mark', salary: 4500,CategoryId:5, TypeId:1}, {name: 'joh', salary: 3450,CategoryId:9, TypeId:1}, {name: 'joe', salary: 5100,CategoryId:1, TypeId:1}, {name: 'moore', salary: 5100,CategoryId:14, TypeId:2}, {name: 'wane', salary: 1500,CategoryId:14, TypeId:2}, {name: 'dick', salary: 400,CategoryId:3, TypeId:1} ];

const categoryOrder = [5,9,14,1,9,3];

const result = [...arr].sort((a,b) => {
  return a.TypeId === b.TypeId
    ? categoryOrder.indexOf(a.CategoryId) - categoryOrder.indexOf(b.CategoryId)
    : a.TypeId - b.TypeId;
});

console.log(result);