2

I have an array:

var arr = [ '4msterdam', 'Par1s', 'N3w York', '2urich'];

How can I sort the array by the number that's contained in each element of the array?

2
  • 3
    What kind of strange application needs this? Commented Mar 5, 2017 at 21:25
  • Łukasz, would you like to accept my answer/ upvote it? Commented Mar 14, 2017 at 15:48

1 Answer 1

1

A good approach would be using Array#sort and RegExp for array sorting, based on the first matched digit in every element.

var arr = ['4msterdam', 'Par1s', 'N3w York', '2urich'];
    sorted = arr.sort((a,b) => a.match(/\d/)[0] - b.match(/\d/)[0]);
    
    console.log(sorted);

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.