Recently encountered an algorithm problem, which requires sorting an array of key:value according to the value pair (the value value here can refer to multiple rows), the logic of a hotel's rating system.
Name Hygiene User Experience Security
A x1 y1 z1
B x2 y2 z2
... ... ... ...
Similar to the above, then First, sort the hygiene. After the hygiene is sorted, select the top three from the hygiene ranking. Select the top three people selected previously. Sort according to the user experience. Select the top two according to the user experience. Sort according to the security. Select the security. of first place.
Finally output the first place.
It feels like they are actually similar, but I have checked the information and the map function, but I still can’t understand how to do it. Please give me some advice. (ps: I obviously feel that my algorithm is not bad, but every time I encounter a slightly more complicated algorithm, I get confused. Not long after I entered the front-end pit, I have gone through the basic js-related codes), please help me Solve doubts.
漂亮男人2017-05-19 10:45:49
// 随机生成数据
// let rand = () => Math.floor(Math.random() * 100)
// let arr = 'ABCDEFG'.split('').map(e => {
// return {
// name: e,
// health: rand(),
// experience: rand(),
// security: rand(),
// }
// })
// console.log(arr)
// 这是随机生成的一组数据
let arr = [ { name: 'A', health: 67, experience: 78, security: 88 },
{ name: 'B', health: 14, experience: 40, security: 32 },
{ name: 'C', health: 91, experience: 31, security: 64 },
{ name: 'D', health: 7, experience: 64, security: 26 },
{ name: 'E', health: 68, experience: 69, security: 77 },
{ name: 'F', health: 91, experience: 44, security: 43 },
{ name: 'G', health: 61, experience: 44, security: 68 } ]
// 排序
let ret = arr
.sort((a, b) => {
return b.health - a.health
})
.slice(0, 3)
.sort((a, b) => {
return b.experience - a.experience
})
.slice(0, 2)
.sort((a, b) => {
return b.security - a.security
})
.shift()
console.log(ret)
// { name: 'E', health: 68, experience: 69, security: 77 }
高洛峰2017-05-19 10:45:49
First of all, is this your topic or project? If it's a real project, you can use lodash
的 sortBy
to sort the objects in the list.
Suppose your hotel list model simplifies to:
const list = [
{ name: 'foo', a: 3, b: 5, c: 7 }, // 这个是酒店模型,a, b, c就是各个因素的打分
...
]
The current requirement is to sort the objects in the list first by a, then by b, and then by c. To implement it is:
let result = _.sortBy(list, o = > o.a); // 先按a排序
result = _.sortBy(list, o => o.b); // 再按b排序
result = _.sortBy(list, o => o.c); // 最后按C排序
If the bigger the score, the better, then it should be in reverse order
let result = _.sortBy(list, o = > -o.a); // 先按a逆序排序
result = _.sortBy(list, o => -o.b); // 再按b逆序排序
result = _.sortBy(list, o => -o.c); // 再按c逆序排序
As mentioned in the question, if you want to pick out 3, 2, and 1, you don’t need to sort all the results every time.
let result = _.sortBy(list, o = > -o.a).slice(3); // 排好序取三个
result = _.sortBy(list, o => -o.b).slice(2);
result = _.sortBy(list, o => -o.c).slice(1);
result[0] // 第一名
If it is an interview question, you still need to complete sortBy
这个函数, 可以简单利用Array#sort
implementation:
function sortBy(list, iterator) {
return list.slice(0).sort(function(left, right) {
left = iterator(left);
right = iterator(right);
return left < right ? -1 : 1;
});
}
Note: sortBy
要实现成稳定排序, 即两个分数一致的对象,排序前后相对位置要保持不变。
当然直接使用上Array#sort(func)
This function is also very convenient.