vue多个优先级排序问题

拿到数据以后要根据三个字段排序
总数据为

res

三个排序字段分别为

in_type
sort_type
val_type

每个字段都是1~10之间的整数,10的优先级最高
需求是全部数据先按照res.in_type排序,排完以后再按照res.in_type排序(第二优先级),最后是按照res.val_type排序。

阅读 3.3k
1 个回答

你的需求是先排 res.in_type,接着排 res.sort_type,最后排 res.val_type

还是以 res.in_type排序,res.in_type 相同以res.sort_type排序,前俩者都相同以res.val_type排序?

第一种:

res.sort((preIn, curIn) => preIn.in_type - curIn.in_type)
  .sort((preSort, curSort) => preSort.sort_type - curSort.sort_type)
  .sort((preVal, curVal) => preVal.val_type - curVal.val_type)

第二种

res.sort(
  (pre, cur) =>
    pre.in_type - cur.in_type ||
    pre.sort_type - cur.sort_type ||
    pre.val_type - cur.val_type
)
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题