对象数组根据某个key来进行排序

  // 自定义排序方法 数组对象按key升序或者降序排列
  const compareValues = (key, order = 'asc') => {
    return function innerSort(a, b) {
      if (!a.hasOwnProperty(key) || !b.hasOwnProperty(key)) return 0;
      // const comparison = a[key]?.localeCompare(b[key]);
      // 这个是按字符串的顺序来进行排列,如果是数组的话,则1 和 10就会排在最前面,
      // 如果是按数字的顺序来进行排列,就要用这种相减的方式
      const comparison = a[key] - b[key]
      console.log(comparison)
      return order === 'desc' ? comparison * -1 : comparison;
    };
  };

用法:

list.sort(compareValues('orderNo'))
// list对象数组根据orderNo来进行排序