如何在javascript中对嵌套的对象数组进行排序?

时间:2019-05-13 03:58:49

标签: javascript arrays reactjs sorting

我有一个对象数组:

entities: [
  {
    name: "zBroomsticks PTY",
    id: 34098365,
    entityType: "personal",
    facilities: [
      {
        type: "Home loan",
        account: "032654 987465",
        existing: true,
        modified: "04/12/2018",
        limit: 100000
      },
      {
        type: "Credt card",
        account: "032654 987465",
        existing: false,
        modified: "04/12/2018",
        limit: 200000
      },
      {
        type: "Credt card",
        account: "032654 987465",
        existing: false,
        modified: "04/12/2018",
        limit: 10000
      },
      {
        type: "Credt card",
        account: "032654 987465",
        existing: false,
        modified: "04/12/2018",
        limit: 10000
      }
    ]
  },
  {
    name: "Mr John Doe -3409865, Mrs Jane Doe -34098365",
    id: 34098365,
    entityType: "business",
    facilities: [
      {
        type: "Overdraft",
        account: "032654 987465",
        existing: false,
        modified: "04/12/2018" ,
        limit: 10000
      }
    ]
  },
  {
    name: "Mr Jack",
    id: 34098365,
    entityType: "mixed",
    facilities: [
      {
        type: "Overdraft",
        account: "032654 987465",
        existing: false,
        modified: "04/12/2018",
        limit: 10000
      }
    ]
  }
]

我想按特定顺序对此进行排序:

  1. entity.name:按字母升序。

  2. entity.entityType:1.个人2.业务3.混合

  3. entity.facilities.limit:降序

这是我到目前为止获得的代码:

sortData(entities) {
    var order = {
      entityType: { personal: 2, business: 1 }
    };

    return entities.map(ent =>
      entities.sort(
        (a, b) =>
          a.name - b.name ||
          order.entityType[a.facilities.entityType] - order.entityType[b.facilities.entityType]
      )
    );
}

我知道如何执行名称排序,但是找不到2和3的方法?

Link to code

3 个答案:

答案 0 :(得分:3)

首先,您可以使用localeCompare()来订购names。其次,entityType数组中没有属性facilities,但是您正在尝试访问它。现在,一种解决方案是首先使用Array.map()获得一个新数组,其中facilities属性由limit属性排序,然后可以对{{1}返回的新数组进行排序}首先是map(),然后是names属性,如下所示:

entityType
const input = [{name:"zBroomsticks PTY",id:34098365,entityType:"personal",facilities:[{type:"Home loan",account:"032654 987465",existing:true,modified:"04/12/2018",limit:100000},{type:"Credt card",account:"032654 987465",existing:false,modified:"04/12/2018",limit:200000},{type:"Credt card",account:"032654 987465",existing:false,modified:"04/12/2018",limit:10000},{type:"Credt card",account:"032654 987465",existing:false,modified:"04/12/2018",limit:10000}]},{name:"Mr John Doe -3409865, Mrs Jane Doe -34098365",id:34098365,entityType:"business",facilities:[{type:"Overdraft",account:"032654 987465",existing:false,modified:"04/12/2018",limit:10000}]},{name:"Mr Jack",id:34098365,entityType:"mixed",facilities:[{type:"Overdraft",account:"032654 987465",existing:false,modified:"04/12/2018",limit:10000}]},{name:"Mr Jack",id:34098365,entityType:"personal",facilities:[{type:"Overdraft",account:"032654 987465",existing:false,modified:"04/12/2018",limit:10000}]}];

let order = {
  entityType: {personal:1, business:2, mixed:3}
};

function sortData(entities)
{    
    let limitOrder = entities.map(e =>
    {
        e.facilities.sort((a, b) => b.limit - a.limit);
        return e;
    });

    return limitOrder.sort((a, b) =>
    {
        return a.name.localeCompare(b.name) ||
               order.entityType[a.entityType] - order.entityType[b.entityType];
    });
}

console.log(sortData(input));

请注意,我使用不同的.as-console {background-color:black !important; color:lime;} .as-console-wrapper {max-height:100% !important; top:0;}复制了与name: "Mr Jack"相关的对象,因此您可以看到当存在两个相等的entityType对象时该算法的性能。

答案 1 :(得分:0)

尝试

eType = {personal:1, business:2, mixed:3};

entities.sort((a,b) => {
   if(a.name>b.name) return 1;
   if(a.name<b.name) return -1;

   let et=eType[a.entityType]-eType[b.entityType];
   return et;   
})

entities.forEach(e=> e.facilities.sort((a,b)=> b.limit - a.limit ))

let entities = [
        {
          name: "zBroomsticks PTY",
          id: 34098365,
          entityType: "personal",
          facilities: [
            {
              type: "Home loan",
              account: "032654 987465",
              existing: true,
              modified: "04/12/2018",
              limit: 100000
            },
            {
              type: "Credt card",
              account: "032654 987465",
              existing: false,
              modified: "04/12/2018",
              limit: 200000
            },
            {
              type: "Credt card",
              account: "032654 987465",
              existing: false,
              modified: "04/12/2018",
              limit: 10000
            },
            {
              type: "Credt card",
              account: "032654 987465",
              existing: false,
              modified: "04/12/2018",
              limit: 10000
            }
          ]
        },
        {
          name: "Mr John Doe -3409865, Mrs Jane Doe -34098365",
          id: 34098365,
          entityType: "business",
          facilities: [
            {
              type: "Overdraft",
              account: "032654 987465",
              existing: false,
              modified: "04/12/2018" ,
              limit: 10000
            }
          ]
        },
{
          name: "Mr Jack",
          id: 34098365,
          entityType: "mixed",
          facilities: [
            {
              type: "Overdraft",
              account: "032654 987465",
              existing: false,
              modified: "04/12/2018",
              limit: 10000
            }
          ]
        }
      ];
      
      
eType = {personal:1, business:2, mixed:3};

entities.sort((a,b) => {
   if(a.name>b.name) return 1;
   if(a.name<b.name) return -1;
   
   let et=eType[a.entityType]-eType[b.entityType];
   return et;   
})

entities.forEach(e=> e.facilities.sort((a,b)=> b.limit - a.limit ))

console.log(entities);

答案 2 :(得分:0)

按照优先级12和最后3的顺序对数组进行排序

2条件,您可以使用index对象将entityType及其权重映射。

3因为要按descending order进行排序,所以您在limit列表中找到了最小的facilities值并将其与其他项目进行比较。

let entities = [
  {
    name: "zBroomsticks PTY",
    id: 34098365,
    entityType: "personal",
    facilities: [
      {
        type: "Home loan",
        account: "032654 987465",
        existing: true,
        modified: "04/12/2018",
        limit: 100000
      },
      {
        type: "Credt card",
        account: "032654 987465",
        existing: false,
        modified: "04/12/2018",
        limit: 200000
      },
      {
        type: "Credt card",
        account: "032654 987465",
        existing: false,
        modified: "04/12/2018",
        limit: 10000
      },
      {
        type: "Credt card",
        account: "032654 987465",
        existing: false,
        modified: "04/12/2018",
        limit: 10000
      }
    ]
  },
  {
    name: "Mr John Doe -3409865, Mrs Jane Doe -34098365",
    id: 34098365,
    entityType: "business",
    facilities: [
      {
        type: "Overdraft",
        account: "032654 987465",
        existing: false,
        modified: "04/12/2018",
        limit: 10000
      }
    ]
  },
  {
    name: "Mr Jack",
    id: 34098365,
    entityType: "mixed",
    facilities: [
      {
        type: "Overdraft",
        account: "032654 987465",
        existing: false,
        modified: "04/12/2018",
        limit: 10000
      }
    ]
  }
];


const entityIndex = { personal: 1, business: 2, mixed: 3 };

let result = entities.sort((a, b) => {
  if (a.name > b.name) return -1;
  if (a.name < b.name) return 1;

  let et = entityIndex[a.entityType] - entityIndex[b.entityType];
  if (et != 0) return et;

  const aMinLimit = Math.min(...a.facilities.map(i => i.limit));
  const bMinLimit = Math.min(...b.facilities.map(i => i.limit));

  return bMinLimit - aMinLimit;

})

console.log(JSON.stringify(result));
相关问题