D
D
deleted-tnorman2014-10-16 01:02:18
JavaScript
deleted-tnorman, 2014-10-16 01:02:18

How to sort an array by number of characters?

There is such an array.
You need to sort it from largest to smallest by the number of characters.

accords: [
      "A\#", "A", "A7", "A\#m", "Am", "Ab", 
      "B","B7", "Bm", "Bb", 
      "C\#", 'C', 'C7', 'Cm', 'C\#m',
      "D\#", 'D', 'D7', 'Dm', 'D\#m', "Db", 
      'E', 'E7', 'Em', "Eb", 
      "F\#", 'F', 'Fm', 'F\#m', 'F7',
      "G\#", "G", "Gm", "G\#m", "Gb", "G7", 
      "H", "Hm", "Hb", "H7"]

Thanks for your help and your time.
Ps
I tried something like this
return this.accords.sort(
                function(a, b){ 
                    return b.length > a.length;
                }
          
      
            );

But something went wrong, please advise how to resolve the issue.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Pushkarev, 2014-10-16
@deleted-tnorman

return b.length - a.length;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question