首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JS按两个值进行排序

JS按两个值进行排序
EN

Stack Overflow用户
提问于 2020-07-28 19:20:35
回答 4查看 62关注 0票数 2

我需要带有Bustype和index的排序数组

我所拥有的

代码语言:javascript
复制
[
  { BusType: "SATA", index: 1, Id: "SATA.1" },
  { BusType: "SATA", index: 10, Id: "SATA.10" },
  { BusType: "IDE", index: 1, Id: "IDE.1" },
  { BusType: "IDE", index: 2, Id: "IDE.2" },
  { BusType: "IDE", index: 10, Id: "IDE.10" },
]

预期产出:

代码语言:javascript
复制
[
  { BusType: "IDE", index: 1, Id: "IDE.1" },
  { BusType: "IDE", index: 2, Id: "IDE.2" },
  { BusType: "IDE", index: 10, Id: "IDE.10" },
  { BusType: "SATA", index: 1, Id: "SATA.1" },
  { BusType: "SATA", index: 10, Id: "SATA.10" }
]

我试过这个:

代码语言:javascript
复制
arr.sort((a, b) => a.Id.localeCompare(b.Id))

但得到了这个:

代码语言:javascript
复制
[
  { BusType: "IDE", index: 1, Id: "IDE.1" },
  { BusType: "IDE", index: 10, Id: "IDE.10" },
  { BusType: "IDE", index: 2, Id: "IDE.2" },
  { BusType: "SATA", index: 1, Id: "SATA.1" },
  { BusType: "SATA", index: 10, Id: "SATA.10" }
]

像字母一样排序..。1 10 100 2 20 3

如何分类正确?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2020-07-28 19:30:51

代码语言:javascript
复制
arr.sort((a, b) => a.BustType.localeCompare(b.BustType) || a.index - b.index);

它将按BustType进行排序,如果返回0 (意味着它们具有相同的BusType),则||的右侧将进入,而a.index - b.index将由index进行排序。这叫做。

票数 1
EN

Stack Overflow用户

发布于 2020-07-28 19:47:49

你可以实现一个多类型的。

你只需要声明一些分类器。这些只是告诉您的字段的排序函数的一种方法,例如,要排序的类型和方向。

下面是一个分类器示例:

代码语言:javascript
复制
/**
 * @param {String} name - Object field to be compared
 * @param {String=['string'],'int'} type - Value type
 * @param {String=['asc'],'desc'} dir - Sort direction
 * @param {Function} fn - Optional comparator function
 */
const exampleSorter = {
  name : 'field',
  type : 'string',
  dir  : 'asc'
  fn   : (a, b) => a - b
}

分类器可以是:

  • 单字符串字段名
  • 一个字段名或排序器对象的混合数组

演示

代码语言:javascript
复制
const busData = [
  { BusType: "SATA" , index: 1  , Id: "SATA.1"  },
  { BusType: "SATA" , index: 10 , Id: "SATA.10" },
  { BusType: "IDE"  , index: 1  , Id: "IDE.1"   },
  { BusType: "IDE"  , index: 2  , Id: "IDE.2"   },
  { BusType: "IDE"  , index: 10 , Id: "IDE.10"  },
]

const multiSort = (data, sorters) => {
  const defaultSorter = { type: 'string', dir: 'asc' }
  const compare = (a, b, sorter) => {
    let field = sorter.name
    if (b == null || b[field] == null) return -1
    if (a == null || a[field] == null) return 1
    if (sorter.fn) return sorter.fn.call(sorter, a[field], b[field])
    switch (sorter.type) {
      case 'int':     return a[field] - b[field];
      case 'string':
      default:        return a[field].localeCompare(b[field])
    }
  }
  if (typeof sorters === 'string') sorters = [sorters]
  sorters = sorters.map(sorter => typeof sorter === 'string'
      ? { ...defaultSorter, name: sorter }
      : { ...defaultSorter, sorter })
  return data.sort((a, b) => {
    for (let i = 0; i < sorters.length; i++) {
      let sorter = sorters[i], result = compare(a, b, sorter)
      if (result !== 0) return result * (sorter.dir === 'desc' ? -1 : 1)
    }
    return 0
  })
}

console.log(multiSort(busData, [
  { name: 'BusType', fn: (a, b) => a.localeCompare(b) },
  { name: 'index', type: 'int', dir: 'asc' }
]))
代码语言:javascript
复制
.as-console-wrapper { top: 0; max-height: 100% !important; }

票数 0
EN

Stack Overflow用户

发布于 2020-07-28 23:12:39

试试这个:

代码语言:javascript
复制
arr.sort(
  (a,b)=> {
    a.BusType<b.BusType?-1:
    a.BusType>b.BusType?1:
    a.index<b.index?-1:
    a.index>b.index?1:
    0;
  }
);

我们比较BusType,如果a's较小,则为-1,如果a's较大,则为1,如果等于:

我们比较index,如果a's较小,则为-1,如果a's较大,则为: 0。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63141351

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档