How to sort array using Sort

Asked

Viewed 27 times

0

I have an array of objects that returns 3 possible sensitivity values: "LOW", "MEDIUM", "HIGH". But with the code below it is ordering in "HIGH - MEDIUM and LOW" respectively in ascending order and I wish it to return "HIGH - MEDIUM and LOW". What can I correct in this code?

In this function I compare the received sensitivities in the array

  orderItemsByOrderOption = (items) => {
switch (this.state.selectedOrderOption.column) {
  case "sensitivity":
    return items.sort((a, b) => {
      if (a.sensitivity > b.sensitivity) {
        return 1;
      }
      if (a.sensitivity < b.sensitivity) {
        return -1;
      }
      // a must be equal to b
      return 0;
    });
  • 1

    Since they are objects you can create an attribute in the object to determine the level of sensitivity, using an integer number for example and sort by this attribute.

  • 1

    This answers your question? Sort array within another array with Reactjs

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.