There is an input array of the form:

var array = [ '<li><input type="radio" name="cityRadio" value="123|Санкт-Петербург">Санкт-Петербург</li>', '<li><input type="radio" name="cityRadio" value="456|Москва">Москва</li>', '<li><input type="radio" name="cityRadio" value="789|Киев">Киев</li>' ]; 

It is necessary to sort it by city names, so that UL> LI comes first with the city of Kiev, then Moscow, then Peter. I tried this:

 array.sort(function (a, b) { return a.match(/(.*)<\/li>/) - b.match(/(.*)<\/li>/); }); 

but not sorted, not strong in regulars

  • Is it impossible to sort it before creating a layout? - Vasily Barbashev
  • I wouldn’t have asked a question then - stckvrw

1 answer 1

 var array = [ '<li><input type="radio" name="cityRadio" value="123|Санкт-Петербург">Санкт-Петербург</li>', '<li><input type="radio" name="cityRadio" value="456|Москва">Москва</li>', '<li><input type="radio" name="cityRadio" value="789|Киев">Киев</li>' ] array.sort(function(a, b) { a = a.replace(/<\/?[^>]*>/g, '') b = b.replace(/<\/?[^>]*>/g, '') return a > b }); console.log(array)