-2

What else can be done to sort the title of a binary array?

let ary = [
  ["search-type", [{
      "title": "google",
      "link": "https://"
    },
    {
      "title": "wikipedia",
      "link": "https://"
    },
    {
      "title": "a-search",
      "link": "https://"
    },
    {
      "title": "c-search",
      "link": "https://"
    },
    {
      "title": "q-search",
      "link": "https://"
    }
  ]],
  ["tool-type", [{
      "title": "remove bg",
      "link": "https://"
    },
    {
      "title": "q",
      "link": "https://"
    },
    {
      "title": "c",
      "link": "https://"
    },
    {
      "title": "3q",
      "link": "https://"
    }
  ]]
];


ary.forEach(array => {
  array[1].sort(function(s, t) {
    let a = s.title.toLowerCase();
    let b = t.title.toLowerCase();
    if (a < b) return -1;
    if (a > b) return 1;
    return 0;
  })
})

console.log(ary);
Purple awn
  • 127
  • 9
  • 1
    What is not working? It looks sorted to me. – epascarello Apr 20 '21 at 17:06
  • @epascarello Hi, I'd like to know if there are any other ways to make it simpler – Purple awn Apr 20 '21 at 17:09
  • What is not simple about it? Seems as simple as you will get. You could use https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare in the sort – epascarello Apr 20 '21 at 17:10
  • @epascarello hasn't the standard `sort()` function been shown to be faster than `localeCompare()`? https://stackoverflow.com/questions/14677060/400x-sorting-speedup-by-switching-a-localecompareb-to-ab-1ab10 – Brandon McConnell Apr 20 '21 at 17:20
  • @BrandonMcConnell Question said nothing about performance.... – epascarello Apr 20 '21 at 17:22
  • @epascarello true, I guess my thought was that `localCompare()` not only performs worse but tends to look more complicated to me. However, I can see how a ternary with logical operators and return values of `-1`, `1`, and `0` might look more complicated to some. – Brandon McConnell Apr 20 '21 at 17:24

2 Answers2

1

You can use localeCompare.

let ary = [["search-type", [{ title: "google", link: "https://" }, { title: "wikipedia", link: "https://" }, { title: "a-search", link: "https://" }, { title: "c-search", link: "https://" }, { title: "q-search", link: "https://" }]], ["tool-type", [{ title: "remove bg", link: "https://" }, { title: "q", link: "https://" }, { title: "c", link: "https://" }, { title: "3q", link: "https://" }]]];

ary.forEach((arr) =>
  arr.forEach((v) => 
    v instanceof Array &&
    v.sort((a, b) => a.title.localeCompare(b.title, undefined, { sensitivity: "base" }))
  )
);

console.log(ary);
Som Shekhar Mukherjee
  • 4,701
  • 1
  • 12
  • 28
0

Yes, this can be simplified. It'll essentially be the same function you wrote, just slimmed down. You can also leave out the 0 return statement.

let ary = [
  ['search-type', [
    { title: 'google', link: 'https://' },
    { title: 'wikipedia', link: 'https://' },
    { title: 'a-search', link: 'https://' },
    { title: 'c-search', link: 'https://' },
    { title: 'q-search', link: 'https://' }
  ]],
  ['tool-type', [
    { title: 'remove bg', link: 'https://' },
    { title: 'q', link: 'https://' },
    { title: 'c', link: 'https://' },
    { title: '3q', link: 'https://' }
  ]]
];

ary.forEach(e => e[1].sort((a,b) => a.title > b.title ? 1 : -1));

console.log(ary);

If you would like to keep the 0 default value for equal strings, you can use this:

let ary = [
  ['search-type', [
    { title: 'google', link: 'https://' },
    { title: 'wikipedia', link: 'https://' },
    { title: 'a-search', link: 'https://' },
    { title: 'c-search', link: 'https://' },
    { title: 'q-search', link: 'https://' }
  ]],
  ['tool-type', [
    { title: 'remove bg', link: 'https://' },
    { title: 'q', link: 'https://' },
    { title: 'c', link: 'https://' },
    { title: '3q', link: 'https://' }
  ]]
];

ary.forEach(e => e[1].sort((a,b) => a.title > b.title ? 1 : (a.title < b.title ? -1 : 0)));

console.log(ary);
Brandon McConnell
  • 5,776
  • 1
  • 20
  • 36