根据多个字段对对象数组进行排序

时间:2021-05-11 05:41:26

标签: javascript

我有一个像

这样的对象数组
const hotels = [
  { hotelName: "a", price: 2000, rating: 3 },
  { hotelName: "b", price: 1000, rating: 2 },
  { hotelName: "c", price: 6000, rating: 3 },
  { hotelName: "d", price: 700, rating: 4 },
  { hotelName: "e", price: 3000, rating: 5 },
];

function sortHotels(value) {
  let sortedHotel= hotels.sort((a, b) => 
    b.rating - a.rating
  );
  
  console.log(sortedHotel)
}

sortHotels(1)
sortHotels(-1)

所以直到现在我都是按照降序对这个 acc 进行排序,但现在我想稍微调整一下。

正如你现在看到的

<块引用>

函数sortHotels

我将“值”作为参数传递,并根据我想排序

例如 - 如果传递的值为 -1。根据评分降序对数据进行排序,如果任何数据的评分相同,则将它们排序为acc。按降序排列到他们的价格

如果传递的值为1,则将数据按评分升序排序,如果任何数据的评分相同,则按acc进行排序。到他们的价格升序。

所以我对在这里的排序方法中添加多个条件感到困惑。

4 个答案:

答案 0 :(得分:1)

你可以这样做:

const hotels = [
  { hotelName: "a", price: 2000, rating: 3 },
  { hotelName: "b", price: 1000, rating: 2 },
  { hotelName: "c", price: 6000, rating: 3 },
  { hotelName: "d", price: 700, rating: 4 },
  { hotelName: "e", price: 3000, rating: 5 },
];

function sortHotels(value) {
  if ( value < 0 )
    return hotels.sort((a, b) => 
      ((b.rating - a.rating) || (b.price - a.price))
    );

  return hotels.sort((a, b) => 
    ((a.rating - b.rating) || (a.price - b.price))
  );
}

console.log(sortHotels(1))
console.log(sortHotels(-1))

// If you'll notice closely we are either using `a - b` or `b - a`
// To simplify the logic you can do something like this

function sortHotelsSmart(value) {
  return hotels.sort((a, b) => 
    ((a.rating - b.rating) || (a.price - b.price)) * value
  );
}

console.log(sortHotelsSmart(1))
console.log(sortHotelsSmart(-1))

仅供参考:排序就地完成并改变实际数组,因此您可能希望对 [...hotels] 使用排序。

答案 1 :(得分:1)

const hotels = [{
    hotelName: "a",
    price: 2000,
    rating: 3
  },
  {
    hotelName: "b",
    price: 1000,
    rating: 2
  },
  {
    hotelName: "c",
    price: 6000,
    rating: 3
  },
  {
    hotelName: "d",
    price: 700,
    rating: 4
  },
  {
    hotelName: "e",
    price: 3000,
    rating: 5
  },
];

function sortHotels(value) {
  if (value === -1) {
    return [...hotels].sort((a, b) => {
      if (a.rating === b.rating) {
        return b.price - a.price;
      } else return b.rating - a.rating;
    });
  } else if (value === 1) {
    return [...hotels].sort((a, b) => {
      if (b.rating === a.rating) {
        return a.price - b.price;
      } else return a.rating - b.rating;
    });
  }
}

console.log(sortHotels(1));
console.log(sortHotels(-1));

答案 2 :(得分:0)

关闭万岁。您可以在排序回调的正文中使用 value 变量。像这样:

(a, b) => ((a.rating - b.rating) || (a.price - b.price)) * value

此回调首先尝试根据评级 (a.rating - b.rating) 确定排序顺序。在相同评级的情况下,减法的结果是 0,这是一个假值。在这种情况下,由于 OR 运算符,基于价格 (a.price - b.price) 的排序顺序将生效。最终结果乘以value参数值,当其值为负时反向排序。

请注意 Array.prototype.sort 方法对数组进行原地排序。您的原始 hotels 数组也将被排序。为避免这种情况,您可以在对原始数组进行排序之前对其进行复制。例如通过使用扩展运算符:[...hotels].

如果您想继续处理已排序的酒店数组,我个人会从 sortHotels 函数返回该排序数组。我也会尽量避免 sortHotels 函数内部的副作用(如写入控制台)。

这将是最终结果:

const hotels = [
  { hotelName: "a", price: 2000, rating: 3 },
  { hotelName: "b", price: 1000, rating: 2 },
  { hotelName: "c", price: 6000, rating: 3 },
  { hotelName: "d", price: 700, rating: 4 },
  { hotelName: "e", price: 3000, rating: 5 },
];

function sortHotels(value) {
  return [...hotels].sort((a, b) =>
    ((a.rating - b.rating) || (a.price - b.price)) * value
  );
}

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

答案 3 :(得分:0)

我希望你正在寻找这样的东西?

const hotels = [
  { hotelName: "a", price: 2000, rating: 3 },
  { hotelName: "b", price: 1000, rating: 2 },
  { hotelName: "c", price: 6000, rating: 3 },
  { hotelName: "d", price: 700, rating: 4 },
  { hotelName: "e", price: 3000, rating: 5 },
];

function sortHotels(value) {
  let sortedHotel= hotels.sort((a, b) => {
  
    if(value === 1) {
        return a.rating - b.rating
    } else {
        return b.rating - a.rating
    }
  }
    
  );
  
  console.log(sortedHotel)
}

sortHotels(1)
sortHotels(-1)

相关问题