如何根据多个条件对对象集合进行排序?

时间:2019-08-29 11:22:26

标签: javascript arrays sorting

我正在尝试使用map方法修复排序问题,因此我为mappedDatasortedData创建了单独的变量。问题是我没有对值进行排序。

第三个对象具有'a'值,因此应按升序排序。

 const mappedData = data.map(number => {
    /* ... */
    number.sortColumn = "";
    if (number.animalNumbersRank) {
      number.sortColumn = number.animalNumbersRank[0].number;
    }
    console.log("number.sortColumn--->", number.sortColumn);
    console.log("number.heightValueStart--->", number.heightValueStart);

    return number;
  });
  const sortedData = mappedData.sort((a, b) => {
    /* ... */
    a.heightValueStart < b.heightValueStart
      ? -1
      : a.heightValueStart > b.heightValueStart
      ? 1
      : a.sortColumn < b.sortColumn
      ? -1
      : a.sortColumn > b.sortColumn
      ? 1
      : 0;
  });

  console.log("data--->", data);
  console.log("sortedData--->", sortedData);

var data = [{
    animalrId: 11111111111,
    // "lionId": erer,
    uiuiuiId: "151856658687700",
    ddfdfdf: "2018-02-14 19:03:31",
    category: "wewe5454",
    organizationName: "apple boy",
    jioID: "fdkldfkl",
    jioIDType: "EIN",
    heightValueStart: "c",
    formattedjioID: "8989-23892389",
    lowerOrgName: "apple boy",
    animalNumbersRank: [{
      number: "c"
    }],
    dklfkldf: [{
      number: "1548267230"
    }],
    jkkklkl: [{
      number: "21468096",
      expired: "Y"
    }],
    sortColumn: "c"
  },

  {
    animalrId: 222222222222,
    lionId: 232323,
    uiuiuiId: "153920398625500",
    ddfdfdf: "2018-10-10 19:02:45",
    category: "wewe 2323",
    organizationName: "b",
    jioID: "fdkldfkl",
    jioIDType: "EIN",
    heightValueStart: "b",
    formattedjioID: "8989-23892389",
    lowerOrgName: "apple boy",
    sortColumn: ""
  },

  {
    animalrId: 33333333333,
    lionId: 232323,
    uiuiuiId: "153920398625500",
    ddfdfdf: "2018-10-10 19:02:45",
    category: "wewe 545445",
    organizationName: "apple boy",
    jioID: "fdkldfkl",
    jioIDType: "EIN",
    heightValueStart: "a",
    formattedjioID: "8989-23892389",
    lowerOrgName: "apple boy",
    animalNumbersRank: [{
      number: "a"
    }],
    dklfkldf: [{
      number: "1548267230"
    }],
    jkkklkl: [{
        number: "475255"
      },
      {
        number: "299993436",
        expired: "Y"
      }
    ],
    sortColumn: "a"
  }
];

const mappedData = data.map(number => {
  /* ... */
  number.sortColumn = "";
  if (number.animalNumbersRank) {
    number.sortColumn = number.animalNumbersRank[0].number;
  }
  console.log("number.sortColumn--->", number.sortColumn);
  console.log("number.heightValueStart--->", number.heightValueStart);

  return number;
});
const sortedData = mappedData.sort((a, b) => {
  /* ... */
  a.heightValueStart < b.heightValueStart ?
    -1 :
    a.heightValueStart > b.heightValueStart ?
    1 :
    a.sortColumn < b.sortColumn ?
    -1 :
    a.sortColumn > b.sortColumn ?
    1 :
    0;
});

console.log("data--->", data);
console.log("sortedData--->", sortedData);

3 个答案:

答案 0 :(得分:1)

enter image description here您缺少回报,请尝试像这样

const sortedData = mappedData.sort(function(a, b){
   if(a.heightValueStart < b.heightValueStart) { return -1; }
   if(a.heightValueStart > b.heightValueStart) { return 1; }
   return 0;
})

答案 1 :(得分:1)

在您的sortedData中,您什么也不会返回,应该return 01-1

const sortedData = data.sort((a, b) => {
  let number = 0;
  if ((a.heightValueStart < b.heightValueStart) || (a.sortColumn < b.sortColumn)) {
    number = -1;
  } else if ((a.heightValueStart > b.heightValueStart) || (a.sortColumn > b.sortColumn)) {
    number = 1;
  }
  return number;
});

检查以下代码段。

const data = [
  {
    animalrId: 11111111111,
    // "lionId": erer,
    uiuiuiId: '151856658687700',
    ddfdfdf: '2018-02-14 19:03:31',
    category: 'wewe5454',
    organizationName: 'apple boy',
    jioID: 'fdkldfkl',
    jioIDType: 'EIN',
    heightValueStart: 'c',
    formattedjioID: '8989-23892389',
    lowerOrgName: 'apple boy',
    animalNumbersRank: [
      {
        number: 'c',
      },
    ],
    dklfkldf: [
      {
        number: '1548267230',
      },
    ],
    jkkklkl: [
      {
        number: '21468096',
        expired: 'Y',
      },
    ],
    sortColumn: 'c',
  },

  {
    animalrId: 222222222222,
    lionId: 232323,
    uiuiuiId: '153920398625500',
    ddfdfdf: '2018-10-10 19:02:45',
    category: 'wewe 2323',
    organizationName: 'b',
    jioID: 'fdkldfkl',
    jioIDType: 'EIN',
    heightValueStart: 'b',
    formattedjioID: '8989-23892389',
    lowerOrgName: 'apple boy',
    sortColumn: '',
  },

  {
    animalrId: 33333333333,
    lionId: 232323,
    uiuiuiId: '153920398625500',
    ddfdfdf: '2018-10-10 19:02:45',
    category: 'wewe 545445',
    organizationName: 'apple boy',
    jioID: 'fdkldfkl',
    jioIDType: 'EIN',
    heightValueStart: 'a',
    formattedjioID: '8989-23892389',
    lowerOrgName: 'apple boy',
    animalNumbersRank: [
      {
        number: 'a',
      },
    ],
    dklfkldf: [
      {
        number: '1548267230',
      },
    ],
    jkkklkl: [
      {
        number: '475255',
      },
      {
        number: '299993436',
        expired: 'Y',
      },
    ],
    sortColumn: 'a',
  },
];

const sortedData = data.sort((a, b) => {
  let number = 0;
  if ((a.heightValueStart < b.heightValueStart) || (a.sortColumn < b.sortColumn)) {
    number = -1;
  } else if ((a.heightValueStart > b.heightValueStart) || (a.sortColumn > b.sortColumn)) {
    number = 1;
  }
  return number;
});

console.log(sortedData); 

答案 2 :(得分:0)

您需要在sortedDate中返回比较结果,因此只需添加一个return

const sortedData = mappedData.sort((a, b) => {
    /* ... */
    return a.heightValueStart < b.heightValueStart
      ? -1
      : a.heightValueStart > b.heightValueStart
      ? 1
      : a.sortColumn < b.sortColumn
      ? -1
      : a.sortColumn > b.sortColumn
      ? 1
      : 0;
  });