js 多维度排序

// 先按deviceState分组,然后每个分组中在按workProgress排序
arr.sort((a, b) => {
  if (a.deviceState === b.deviceState) {
    return a.workProgress === null
      ? 1
      : b.workProgress === null
      ? -1
      : Number(a.workProgress) > Number(b.workProgress)
      ? -1
      : 1
  } else {
    return map[a.deviceState] - map[b.deviceState]
  }
})

先按deviceState分组,然后每个分组中再按workProgress排序(null值过滤出来)

你可能感兴趣的:(js 多维度排序)