Javascript 使用自定义函数对数组排序

Javascript 使用自定义函数对数组排序,javascript,Javascript,我创建此代码是为了根据carvalue对数组值进行排序 const arr=[ { 汽车:“奥迪”, 年龄:2015 }, { 汽车:“宝马”, 年龄:1999 }, { 汽车:“阿尔法”, 年龄:2019 }, ]; 函数createSort(属性){ 返回函数比较(a,b){ 返回a[属性]x.car.localeCompare(y.car)); 控制台日志(输出)短代码: arr.sort((x, y) =>x.car.localeCompare(y.car)); 完整代码:

我创建此代码是为了根据
car
value对数组值进行排序

const arr=[
{
汽车:“奥迪”,
年龄:2015
},
{
汽车:“宝马”,
年龄:1999
},
{
汽车:“阿尔法”,
年龄:2019
},
];
函数createSort(属性){
返回函数比较(a,b){
返回a[属性]排序如下:

   const arr = [
      {
          car: 'audi',
          age: 2015
      },
      {
          car: 'bmw',
          age: 1999
      },
      {
          car: 'alfa',
          age: 2019
      },
    ];
    function createSort(property) {
      return function compareString(a,b) {
          return a[property] > b[property]
      }
    }

    const sortByTitle = createSort('car');
    arr.sort(sortByTitle);

    console.log(arr);
排序如下:

   const arr = [
      {
          car: 'audi',
          age: 2015
      },
      {
          car: 'bmw',
          age: 1999
      },
      {
          car: 'alfa',
          age: 2019
      },
    ];
    function createSort(property) {
      return function compareString(a,b) {
          return a[property] > b[property]
      }
    }

    const sortByTitle = createSort('car');
    arr.sort(sortByTitle);

    console.log(arr);

这是您可以使用的通用函数

    function createSort (field) {
      return function sort (data1, data2) {
      let value1 = data1[field]
      let value2 = data2[field]
      return (value1 == null && value2 != null) ? -1
      : (value1 != null && value2 == null) ? 1
        : (value1 == null && value2 == null) ? 0
          : (typeof value1 === 'string' && typeof value2 === 'string') ? value1.localeCompare(value2)
            : (value1 < value2) ? -1 : (value1 > value2) ? 1 : 0
     }
    }
函数createSort(字段){
返回函数排序(数据1、数据2){
让value1=data1[字段]
让value2=data2[字段]
返回值(value1==null&&value2!=null)?-1
:(value1!=null&&value2==null)?1
:(value1==null&&value2==null)?0
:(typeof value1==='string'&&typeof value2=='string')?value1.localeCompare(value2)
:(value1value2)?1:0
}
}

这是您可以使用的通用函数

    function createSort (field) {
      return function sort (data1, data2) {
      let value1 = data1[field]
      let value2 = data2[field]
      return (value1 == null && value2 != null) ? -1
      : (value1 != null && value2 == null) ? 1
        : (value1 == null && value2 == null) ? 0
          : (typeof value1 === 'string' && typeof value2 === 'string') ? value1.localeCompare(value2)
            : (value1 < value2) ? -1 : (value1 > value2) ? 1 : 0
     }
    }
函数createSort(字段){
返回函数排序(数据1、数据2){
让value1=data1[字段]
让value2=data2[字段]
返回值(value1==null&&value2!=null)?-1
:(value1!=null&&value2==null)?1
:(value1==null&&value2==null)?0
:(typeof value1==='string'&&typeof value2=='string')?value1.localeCompare(value2)
:(value1value2)?1:0
}
}
短代码:

arr.sort((x, y) =>x.car.localeCompare(y.car));
完整代码:

const arr=[
{
汽车:“奥迪”,
年龄:2015
},
{
汽车:“宝马”,
年龄:1999
},
{
汽车:“阿尔法”,
年龄:2019
},
];
让输出=arr.sort((x,y)=>x.car.localeCompare(y.car));
控制台日志(输出)短代码:

arr.sort((x, y) =>x.car.localeCompare(y.car));
完整代码:

const arr=[
{
汽车:“奥迪”,
年龄:2015
},
{
汽车:“宝马”,
年龄:1999
},
{
汽车:“阿尔法”,
年龄:2019
},
];
让输出=arr.sort((x,y)=>x.car.localeCompare(y.car));

控制台日志(输出)编辑:@David answer似乎是一个更好的方法

var-arr=[
{
汽车:“奥迪”,
年龄:2015
},
{
汽车:“宝马”,
年龄:1999
},
{
汽车:“阿尔法”,
年龄:2019
},
];
arr.sort(函数(a,b){
如果(a.carb.car){返回1;}
返回0;
})

console.log(arr)
编辑:@David answer似乎是一个更好的方法

var-arr=[
{
汽车:“奥迪”,
年龄:2015
},
{
汽车:“宝马”,
年龄:1999
},
{
汽车:“阿尔法”,
年龄:2019
},
];
arr.sort(函数(a,b){
如果(a.carb.car){返回1;}
返回0;
})

console.log(arr)
要比较字符串,请使用:
返回a[property].localeCompare(b[property])
。有关的问题很多。若要比较字符串,请使用:
返回[property].localeCompare(b[property])
。有大量关于的问题。代码不排序代码不排序