【问题标题】:how to set order by (specific order) in Underscore js - Arrays如何在Underscore js中设置顺序(特定顺序) - 数组
【发布时间】:2019-11-06 13:46:41
【问题描述】:

我有一个数组,我需要基于自定义排序顺序 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> 

我希望结果集的特定顺序为
CategoryId:(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,
}];

【问题讨论】:

  • 按照哪个规则排序:(5,9,14,1,9,3) ?
  • 我需要输出格式
  • 5、9、14、1、9、3的逻辑是什么?只是一个硬编码的目标?什么标准决定哪一个 9 应该放在这两个 9 位置中的哪一个?
  • 我已经更新了代码。我需要根据 TypeID 进行分组,我需要按特定 categoryId (5,9,14,1,9,3) 排序
  • 输出应该是什么样子?分组应该是什么样的?

标签: javascript arrays sorting underscore.js


【解决方案1】:

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

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);

【讨论】:

    【解决方案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)
    

    这能回答你的问题吗?

    【讨论】:

    • 是的。几乎正确,我需要像 TypeID: (1) 这样的输出 - order by : 5,9,14,1,9,3, TypeID: 2 Order by : 5,9,14,11,9,3。以后如果数据的TypeID为3,会这样。
    • 我可以将 typeId 传递给像sort(typeId, order, array) 这样的排序函数吗?
    【解决方案3】:

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

    sortBy_.sortBy(list, iteratee, [context]) 返回列表的(稳定)排序副本,按通过 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>
    

    【讨论】:

    • 基于 PO 要求的排序应以专用数组中定义的特定方式实现。
    猜你喜欢
    • 1970-01-01
    • 2018-03-28
    • 1970-01-01
    • 2011-05-16
    • 1970-01-01
    • 2012-02-01
    • 2021-09-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多