代码之家  ›  专栏  ›  技术社区  ›  Mahesh G

对具有Object数组的JavaScript数组进行排序

  •  -2
  • Mahesh G  · 技术社区  · 5 年前

    你能建议我根据部分名称按优先级对以下数组进行排序的最佳方法吗。我更担心的是时间的复杂性,因为我的数组实际上由10万条记录组成。

    如果有更好的存储方式,我也可以更改数组结构

    [{
        id: 'field1',
        sections: [{
            name: 'Top_Section',
            priority: 3
          },
          {
            name: 'Bottom_Section',
            priority: 3
          }
        ]
      },
      {
        id: 'field2',
        sections: [{
            name: 'Top_Section',
            priority: 2
          },
          {
            name: 'Bottom_Section',
            priority: 4
          }
        ]
      },
      {
        id: 'field3',
        sections: [{
            name: 'Top_Section',
            priority: 1
          },
          {
            name: 'Bottom_Section',
            priority: 1
          }
        ]
      },
      {
        id: 'field4',
        sections: [{
            name: 'Top_Section',
            priority: 4
          },
          {
            name: 'Bottom_Section',
            priority: 2
          }
        ]
      }
    ];
    

    就像我想根据Top_Section对优先级进行排序一样,我的预期输出应该如下 因为字段3具有优先级1,字段2具有优先级2,以此类推。

    [
      {
        id: 'field3',
        sections: [
          { name: 'Top_Section', priority: 1 },
          { name: 'Bottom_Section', priority: 1 }
        ]
      },
      {
        id: 'field2',
        sections: [
          { name: 'Top_Section', priority: 2 },
          { name: 'Bottom_Section', priority: 4 }
        ]
      },
      {
        id: 'field1',
        sections: [
          { name: 'Top_Section', priority: 3 },
          { name: 'Bottom_Section', priority: 3 }
        ]
      },
      {
        id: 'field4',
        sections: [
          { name: 'Top_Section', priority: 4 },
          { name: 'Bottom_Section', priority: 2 }
        ]
      }
    ];
    
    2 回复  |  直到 5 年前
        1
  •  0
  •   Rickard Elimää    5 年前

    我在这里假设“Top_Section”始终位于sections数组的第一个位置。

    我还假设只有两种类型的优先级:“顶部_部分”和“底部_部分”

    let list = [{
        id: 'field1',
        sections: [{
            name: 'Top_Section',
            priority: 3
          },
          {
            name: 'Bottom_Section',
            priority: 3
          }
        ]
      },
      {
        id: 'field2',
        sections: [{
            name: 'Top_Section',
            priority: 2
          },
          {
            name: 'Bottom_Section',
            priority: 4
          }
        ]
      },
      {
        id: 'field3',
        sections: [{
            name: 'Top_Section',
            priority: 1
          },
          {
            name: 'Bottom_Section',
            priority: 1
          }
        ]
      },
      {
        id: 'field4',
        sections: [{
            name: 'Top_Section',
            priority: 4
          },
          {
            name: 'Bottom_Section',
            priority: 2
          }
        ]
      }
    ];
    
    function sortBy(priorityName) {
      let priorityPosition = (priorityName == 'Top_Section') ? 0 : 1;
      
      return (a, b) => {
        return a['sections'][priorityPosition].priority - b['sections'][priorityPosition].priority;
      }
    }
    
    console.log( list.sort(sortBy('Top_Section')) );
        2
  •  -1
  •   Lajos Arpad    5 年前

    让我们创建一个比较器

    function compare(a, b) {
        var sumA = 0;
        var sumB = 0;
        for (var section of a.sections) sumA += section.priority;
        for (var section of b.sections) sumB += seciton.priority;
        return sumB - sumA;
    }
    
    arr.sort(compare);
    

    如果第一个参数较大,则比较器返回正,如果第二个参数较大则返回负,如果它们相等则返回0。我假设优先级之和的数值越低,项目就越大。