老人理发:
第一行是老人的序号
第二行是老人的性别:1代表女性,0代表男性
第三行是老人的年龄
女性优先,年龄大者优先
输出理发序列
例子:
输入:
1 2 3 4 5
1 0 1 0 1
65 67 87 76 98
输出:
5 3 1 4 2
N = '1 2 3 4 5'
M = '1 0 1 0 1'
Age = '65 67 87 76 98'
let arr_n=N.split(' ');
let arr_m=M.split(' ');
let arr_age=Age.split(' ');
console.log(arr_n,arr_m,arr_age);
arr.push(arr_n);
arr.push(arr_m);
arr.push(arr_age);
let obj={};
let arr_obj=[];
// 如何遍历输出多个对象,构造函数
function Obj(id,sex,age){
this.id=id;
this.sex=sex;
this.age=age;
}
for(i=0;i<arr_n.length;i++){
// obj.id=arr_n[i];
// obj.sex=arr_m[i];
// obj.age=arr_age[i];
arr_obj.push(new Obj(arr_n[i],arr_m[i],arr_age[i]))
}
// 将对象按照某一个属性排序
function sortBy(field) {
return (x, y) => {
return x[field] - y[field];
}
}
arr1=arr_obj.sort(sortBy("age"));
let arr_male=[];
let arr_female=[];
arr_obj.some(item=> {
if (item.sex == 0) {
arr_male.push(item)
}else{
arr_female.push(item)
}
})
// console.log(arr_male,arr_female);
obj_result=arr_male.sort(sortBy("age")).concat(arr_female.sort(sortBy("age")));
// console.log(obj_result)
for(j=arr_obj.length-1;j>=0;j--){
console.log(obj_result[j].id)
}