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.
// 隨機(jī)生成數(shù)據(jù)
// 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)
// 這是隨機(jī)生成的一組數(shù)據(jù)
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 }
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 }, // 這個(gè)是酒店模型,a, b, c就是各個(gè)因素的打分
...
]
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); // 排好序取三個(gè)
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
這個(gè)函數(shù), 可以簡(jiǎn)單利用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
要實(shí)現(xiàn)成穩(wěn)定排序, 即兩個(gè)分?jǐn)?shù)一致的對(duì)象,排序前后相對(duì)位置要保持不變。
當(dāng)然直接使用上Array#sort(func)
This function is also very convenient.