Home > Software design >  Sorting by either multiple or single criteria failing
Sorting by either multiple or single criteria failing

Time:11-26

I have the following example data that I'm trying to sort by multiple criteria:

  1. details.length (from smaller to bigger)
  2. details.type (alphabetically : Claimant, FellowPassenger)

If I sort it by details.length it seems to work but details.type doesn't both on multiple or single sorting criteria versions as data isn't sorted alphabetically (Claimant should appear first than FellowPassenger).

So the output should be:

 sortedByMultiple:  [
  {
    "document_file_name": "4020672_FileName.pdf",
    "details": [
      {
        "id": 20656,
        "type": "Claimant",
        "name": "First Name Last Name"
      }
    ],
    "state": "rejected"
  },
  {
    "document_file_name": "4020890_FileName.pdf",
    "details": [
      {
        "id": 10000,
        "type": "Fellow",
        "name": "Fellow First Name Last Name"
      }
    ],
    "state": "rejected"
  },
  {
    "document_file_name": "4020600_FileName.pdf",
    "details": [
      {
        "id": 10000,
        "type": "Fellow",
        "name": "Fellow First Name Last Name"
      },
      {
        "id": 20656,
        "type": "Claimant",
        "name": "First Name Last Name"
      }
    ],
    "state": "accepted"
  }
]

const groupedStackOverflow = [
  {
    "document_file_name": "4020600_FileName.pdf",
    "details": [
      {
        "id": 10000,
        "type": "Fellow",
        "name": "Fellow First Name Last Name"
      },
      {
        "id": 20656,
        "type": "Claimant",
        "name": "First Name Last Name"
      }
    ],
    "state": "accepted"
  },
  {
    "document_file_name": "4020890_FileName.pdf",
    "details": [
      {
        "id": 10000,
        "type": "Fellow",
        "name": "Fellow First Name Last Name"
      }
  ],
    "state": "rejected"
  },
  {
    "document_file_name": "4020672_FileName.pdf",
    "details": [
    {
        "id": 20656,
        "type": "Claimant",
        "name": "First Name Last Name"
      }
    ],
    "state": "rejected"
  }
]

console.log("groupedStackOverflow: ",groupedStackOverflow )


const sortedByMultiple = groupedStackOverflow.sort(function (a, b) {
  return a.details.length - b.details.length || a.details.type - b.details.type ;
});

console.log("sortedByMultiple: ", sortedByMultiple);


const sortedByOne = groupedStackOverflow.sort(function (a, b) {
  return a.details.type - b.details.type ;
});

console.log("sortedByOne: ", sortedByOne);

CodePudding user response:

Try changing your sort condition using a string comparison for the details.type because a.details.type - b.details.type return NaN. Try replace it with …type.charCodeAt()

CodePudding user response:

  • Firstly, We have sorted the array based on the details object length.
  • After that, we compared object details.type with the help of String.prototype.localeCompare() method for sorting the types.

const groupedStackOverflow = [{
    "document_file_name": "4020600_FileName.pdf",
    "details": [{
        "id": 10000,
        "type": "Fellow",
        "name": "Fellow First Name Last Name"
      },
      {
        "id": 20656,
        "type": "Claimant",
        "name": "First Name Last Name"
      }
    ],
    "state": "accepted"
  },
  {
    "document_file_name": "4020890_FileName.pdf",
    "details": [{
      "id": 10000,
      "type": "Fellow",
      "name": "Fellow First Name Last Name"
    }],
    "state": "rejected"
  },
  {
    "document_file_name": "4020672_FileName.pdf",
    "details": [{
      "id": 20656,
      "type": "Claimant",
      "name": "First Name Last Name"
    }],
    "state": "rejected"
  }
]


// sorting based upon index
const sortedByLength = groupedStackOverflow.sort((a, b) => {
  return a.details.length - b.details.length;
});
// sorting based upon details type alphbatically
const finalSorted = groupedStackOverflow.map(item => {
  if (item.details.length > 1) {
    return item.details.sort((a, b) => {
      // we are using localeCompare() method to sort details.type
      return (a.type).localeCompare(b.type);
    });
  } else {
    return item;
  }
})
console.log(finalSorted);

  • Related