Sort an array of numeric, alphabetic and alphanumeric values

Thanks to japanfever who basically reached this one line approach!

9 replies
  1. japanfever
    japanfever says:


    // some cases are not good, example...
    var array = [ 'japan1', '1', 'japan10', '10', 'japan2', '2' ];

    // with your code...
    const numbers = array.filter(a => !isNaN(a)).sort((a, b) => a - b);
    const alphanumerics = array.filter(a => isNaN(a)).sort((a, b) => a =48 && i bb) ? 1 : -1;
    }
    }
    return a.length - b.length;
    });

    for (var z = 0; z < this.length; z++)
    this[z] = this[z].join("");
    }

    // using...
    array.alphanumSort();

    // now the output is good...
    // [ '1', '2', '10', 'japan1', 'japan2', 'japan10' ]

    // ----

    // OR MAYBE YOU LIKE A GOOD ONE-LINE SOLUTION... OK, HERE IT IS (BUT SLOW)
    var array = [ 'japan1', '1', 'japan10', '10', 'japan2', '2' ];
    array.sort((new Intl.Collator(0,{numeric:true})).compare);

    // JAPANFEVER HAS DONE IT BETTER AGAIN!, signed:
    // E3839EE383AAE382AAE9B3A9E38388E383ACE383AD

    • Génesis
      Génesis says:

      Yeah you are right and I like the one line solution, however I deleted some parentheses and the new ^^

  2. japanfever
    japanfever says:

    Shit! your blog cut my code again! I can see the one-line solution code, but the fastest code no can see it!! I’ve used the CODE tags!! shit blog!! I think I won’t write here again!! You win, you are better troll than me!!

    • Génesis
      Génesis says:

      What did you use? It’s strange you just have to use:
      < c o d e > this.code; < / c o d e >
      (but without spaces between)

      For example:

      // Before this comment is the code start tag < c o d e >
      let array = [ 'a2', '22', 'a10', '10', 'a11', '1' ]

      document.body.textContent = array.sort(Intl.Collator(0, {numeric: true}).compare)

      // More code
      let array = [5, 1, 11, '2', 'girl69', 79, 'boy6', 'tesla', 0]

      const array_ordered = array.sort((a, b) => {
      if (!isNaN(a) && !isNaN(b))
      return a - b
      else if (isNaN(a) && isNaN(b))
      return a < b ? -1 : 1 else if (isNaN(a) && !isNaN(b)) return 1 else return -1 }) // After this comment is the code end tag < / c o d e >

      It’s a normal WordPress post japanfever ^^ don’t get mad for that dude, sometimes we got mistakes like I did sorting 😉 (on my defence I was preparing a pitch)!!

      Try again like I told you, use a random code and the code tags, if it’s a Worpress code tag problem I will change to Disqus xD or you can have a JSFiddle account (for test and examples like me) and save (register) all your progress 🙂

    • Génesis
      Génesis says:

      There wasn’t anything posted apart from the code and of course the comments, I updated it with your recoginition, the merit is yours dude!

    • Génesis
      Génesis says:

      If your approach is better than mine (also talking about complexity and readability) I will update it and give you the recoginition like I did in other posts. At the end I just wanna spread how powerful and easy JS can be xD

Comments are closed.