基于Javascript中的多个条件对数组进行排序

时间:2018-03-21 11:06:19

标签: javascript sorting

我有一个包含如下对象的数组:

[{
    id: 34,
    users: [{
        name: 'Lisa',
        age: 23
    }, {
        name: 'Steven',
        age: 24
    }]
} ,{
    id: 35,
    users: [{
        name: 'John',
        age: 23,
    }, {
        name: 'Steven',
        age: 24
    }, {
        name: 'Charlie',
        age: 24
    }]
}, {
    id: 36,
    users: [{
        name: 'Lisa',
        age: 23,
    }, {
        name: 'John',
        age: 24
    }, {
        name: 'Homer',
        age: 24
    }, {
        name: 'Charlie',
        age: 24
    }]
}, {
    id: 37,
    users: [{
        name: 'Lisa',
        age: 23
    }, {
        name: 'John',
        age: 24
    }]
}]

我想根据几个条件(优先级)对数组进行排序: 首先,我希望用户名为' John'和' Lisa'用户数少于3 其次,我希望用户名为' John'和' Lisa'无论参加人数多少 然后剩下的对象

所以,我作为例子的数组将成为:

[{
    id: 37,
    users: [{
        name: 'Lisa',
        age: 23
    }, {
        name: 'John',
        age: 24
    }]
}, {
    id: 36,
    users: [{
        name: 'Lisa',
        age: 23
    }, {
        name: 'John',
        age: 24
    }, {
        name: 'Homer',
        age: 24
    }, {
        name: 'Charlie',
        age: 24
    }]
}, {
    id: 34,
    users: [{
        name: 'Lisa',
        age: 23
    }, {
        name: 'Steven',
        age: 24
    }]
}, {
    id: 35,
    users: [{
        name: 'John',
        age: 23
    }, {
        name: 'Steven',
        age: 24
    }, {
        name: 'Charlie',
        age: 24
    }]
}]

我现在有这个,根据参与者的姓名和数量进行适当的排序。但如果参与者的数量不匹配,那么它并不关心要排序的名称,这是错误的:

 const names = ['John', 'Lisa']
 unorderedLeagues.sort((a, b) => {
    const aUserIncluded = every(names, priorityName =>
      some(a.users, { name: priorityName }),
    );
    const bUserIncluded = every(names, priorityName =>
      some(b.users, { name: priorityName }),
    );

    return (
      (bUserIncluded && b.users.length <= 3) -
      (aUserIncluded && a.users.length <= 3)
    );
  });

我应该如何修改此排序以执行我所描述的优先级类型?

3 个答案:

答案 0 :(得分:1)

您可以使用布尔值来检查所需名称是否在users中。然后考虑长度与否。

var array = [{ id: 34, users: [{ name: 'John', age: 23}, { name: 'Steven', age: 24}] }, { id: 35, users: [{ name: 'John', age: 23}, { name: 'Steven', age: 24}, { name: 'Charlie', age: 24}] }, { id: 36, users: [{ name: 'Lisa', age: 23}, { name: 'John', age: 24}, { name: 'Homer', age: 24}, { name: 'Charlie', age: 24}] }, { id: 37, users: [{ name: 'Lisa', age: 23}, { name: 'John', age: 24}] }],
    find = ['John', 'Lisa'];

array.sort(function (a, b) {
    var aAll = find.every(n => a.users.find(({ name }) => name === n)),
        bAll = find.every(n => b.users.find(({ name }) => name === n));

    return (bAll && b.users.length === 2) - (aAll && a.users.length === 2) || bAll - aAll;
});

console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:0)

此方法应该完成这项工作:

&#13;
&#13;
var unsorted = [{
    id: 34,
    users: [{
        name: 'Lisa',
        age: 23
      },
      {
        name: 'Steven',
        age: 24
      }
    ]
  },
  {
    id: 35,
    users: [{
        name: 'John',
        age: 23
      },
      {
        name: 'Steven',
        age: 24
      },
      {
        name: 'Charlie',
        age: 24
      }
    ]
  },
  {
    id: 36,
    users: [{
        name: 'Lisa',
        age: 23
      },
      {
        name: 'John',
        age: 24
      },
      {
        name: 'Homer',
        age: 24
      },
      {
        name: 'Charlie',
        age: 24
      }
    ]
  },
  {
    id: 37,
    users: [{
        name: 'Lisa',
        age: 23
      },
      {
        name: 'John',
        age: 24
      }
    ]
  }
];

var sorted = unsorted.sort((a, b) => sortMethod(a, b));
console.log(sorted);

function sortMethod(a, b) {
  var con1a = a.users.find(u => u.name == 'Lisa') && a.users.find(u => u.name == 'John');
  var con1b = b.users.find(u => u.name == 'Lisa') && b.users.find(u => u.name == 'John');

  if (con1a && !con1b) return -1;
  if (!con1a && con1b) return 1;
  return a.users.length - b.users.length;

}
&#13;
&#13;
&#13;

旁注:我必须首先重新格式化无效的JSON,才能成功解析它。

答案 2 :(得分:0)

尝试这样的事情

yourObj.sort(function(curr,next){
    return ((curr.users.filter(x=>x.name=='John'|| x.name=='Steven').length > 0) && curr.users.length < 3 )? 1 : 2;
})

var arrObj= [{
    id: 34,
    users: [{
        name: 'Lisa',
        age: 23
    }, {
        name: 'Steven',
        age: 24
    }]
} ,{
    id: 35,
    users: [{
        name: 'John',
        age: 23,
    }, {
        name: 'Steven',
        age: 24
    }, {
        name: 'Charlie',
        age: 24
    }]
}, {
    id: 36,
    users: [{
        name: 'Lisa',
        age: 23,
    }, {
        name: 'John',
        age: 24
    }, {
        name: 'Homer',
        age: 24
    }, {
        name: 'Charlie',
        age: 24
    }]
}, {
    id: 37,
    users: [{
        name: 'Lisa',
        age: 23
    }, {
        name: 'John',
        age: 24
    }]
}];


arrObj.sort(function(curr,next){
    return ((curr.users.filter(x=>x.name=='John'|| x.name=='Steven').length > 0) && curr.users.length < 3 )? 1 : 2;
});

console.log(arrObj);