1

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

Alex Murray
  • 265
  • 6
  • 21
  • Possible duplicate of [Sort array of objects by string property value in JavaScript](http://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value-in-javascript) – Hanlet Escaño Dec 09 '15 at 21:02
  • 1
    Because `finalArray` has only one entry, the `cityArray` which you've turned into a string for some reason. – David Thomas Dec 09 '15 at 21:03
  • yeah i changed to have the sort on cityarray and that worked. thanks – Alex Murray Dec 09 '15 at 21:06

1 Answers1

3

You need cityArray to sort before joining.

cityArray.sort();
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392