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

3
  • Possible duplicate of Sort array of objects by string property value in JavaScript Commented Dec 9, 2015 at 21:02
  • 1
    Because finalArray has only one entry, the cityArray which you've turned into a string for some reason. Commented Dec 9, 2015 at 21:03
  • yeah i changed to have the sort on cityarray and that worked. thanks Commented Dec 9, 2015 at 21:06

1 Answer 1

3

You need cityArray to sort before joining.

cityArray.sort();
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.