const obj = {
name: 'Alice',
age: 24,
gender: 'female',
city: 'New York',
}
const sortKeysByConditions = (obj, firstKey, secondKey) => {
const sortedKeys = Object.keys(obj).sort((a, b) => {
if (a === firstKey) return -1;
if (b === firstKey) return 1;
if (obj[a][firstKey] > obj[b][firstKey]) return -1;
if (obj[a][firstKey] < obj[b][firstKey]) return 1;
if (obj[a][secondKey] > obj[b][secondKey]) return -1;
if (obj[a][secondKey] < obj[b][secondKey]) return 1;
return 0;
});
return sortedKeys;
}
// 按age和name排序,优先按age,再按name
const sortedKeys = sortKeysByConditions(obj, 'age', 'name');
console.log(sortedKeys); // ['age', 'name', 'city', 'gender']