Alex Murray
Alex Murray

Reputation: 275

Sorting an array alphabetically Javascript

I'm trying to collect a list and put them into an array then sort via the first letter of each item in the array. So far i have:

var cityArray = [];
  $("#addresses_list ul li .name").each(function() { cityArray.push($(this).text().trim()) });
  var finalArray = ['"' + cityArray.join('", "') + '"'];
  finalArray.sort();
  alert(finalArray);

This is collecting correctly and grouping into an array but still not sorting. Any idea why it's not? Thanks in advance

Upvotes: 1

Views: 1330

Answers (1)

Nina Scholz
Nina Scholz

Reputation: 386550

You need cityArray to sort before joining.

cityArray.sort();

Upvotes: 3

Related Questions