0

I have an array like this:

const test = [
  {
    label: "C",
    options: [
      { label: "C"},
      { label: "B"}
    ]
  },
  {
    label: "A",
    options: [
      { label: "Z"},
      { label: "Y"}
    ]
  },
  {
    label: "B",
    options: [
      { label: "J"},
      { label: "H"}
    ]
  },
  {
    label: "D",
    options: [
      { label: "T"},
      { label: "B"}
    ]
  }
]

I need to sort alphabetically this array in two ways:

1) By the first label (outside the options array)

2) And the options by label

I only managed to sort by the first label so far, using this example:

function compareStrings(a, b) {
  a = a.toLowerCase();
  b = b.toLowerCase();
  return (a < b) ? -1 : (a > b) ? 1 : 0;
}

test.sort(function (a, b) {
  return compareStrings(a.label, b.label)
})

How can I order the options inside as well?


The expected output array would be:

const test_SORTED = [
  {
    label: "A",
    options: [
      { label: "B"},
      { label: "C"}
    ]
  },
  {
    label: "B",
    options: [
      { label: "Y"},
      { label: "Z"}
    ]
  },
  {
    label: "C",
    options: [
      { label: "H"},
      { label: "J"}
    ]
  },
  {
    label: "D",
    options: [
      { label: "B"},
      { label: "T"}
    ]
  }
]
3
  • can you mention the expected output array Commented Mar 19, 2020 at 4:37
  • @RajeshVerma done Commented Mar 19, 2020 at 4:41
  • I got your expected output array. You need to sort the options independent of the parent object Commented Mar 19, 2020 at 4:50

2 Answers 2

3

While sorting the test array, I have sorted the options array also.

const test = [
  {
    label: "C",
    options: [
      { label: "C"},
      { label: "B"}
    ]
  },
  {
    label: "A",
    options: [
      { label: "Z"},
      { label: "Y"}
    ]
  },
  {
    label: "B",
    options: [
      { label: "J"},
      { label: "H"}
    ]
  },
  {
    label: "D",
    options: [
      { label: "T"},
      { label: "B"}
    ]
  }
];
function compareStrings(a, b) {
  a = a.toLowerCase();
  b = b.toLowerCase();
  return (a < b) ? -1 : (a > b) ? 1 : 0;
}

console.log(
    test.sort(function (a, b) {
        a.options = a.options.sort(function (c, d) {
            return compareStrings(c.label, d.label)
        })
        b.options = b.options.sort(function (e, f) {
            return compareStrings(e.label, f.label)
        })
        return compareStrings(a.label, b.label)
    })
)

Sign up to request clarification or add additional context in comments.

2 Comments

If you're going to use a code snippet, remember to use a console.log to display the output.
Sure, I got it. Thanks @DaneBrouwer
0

You should sort each of the element's options separately with a forEach loop to avoid sorting them multiple times when doing in sort method.

const comparator = (a, b) => (a.label < b.label ? -1 : a.label > b.label ? 1 : 0);
test.sort(comparator);
test.forEach(el => el.options.sort(comparator));

Also, sort changes the array directly, so no need of assigning values to options.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.