I'm building an app which involves a jQuery mobile directory, which loads three sections of names from a JSON file. I'm wondering if there's a way to automatically alphabetize the items from the JSON array. The people who have commissioned the app will need to add items in future, and it would be easier for them if they didn't have to worry about adding items in alphabetical order themselves. I've put slightly different code here, because of IP, but it should work the same way. To clarify, the proper code will only have "name", not "firstName" and lastName", so sorting by last name won't be necessary.
Edit: I realise this question is a duplicate, and I'm not sure I can prove how it's unique, but the solution that worked for me was not given on the other page, so that page wouldn't have answered my question.
html:
<div data-role="page" id="home">
<div data-role="header">
<h1>Star Trek: The Next Generation</h1>
</div>
<div data-role="main" class="ui-content">
<div data-role="collapsible" data-collapsed="true">
<h2>Humans</h2>
<ul id="humans" data-role="listview" data-filter="true" data-inset="true" data-icon="user" data-autodividers="true">
</ul>
</div>
<div data-role="collapsible" data-collapsed="true">
<h2>Robots</h2>
<ul id="robots" data-role="listview" data-filter="true" data-inset="true" data-icon="gear" data-autodividers="true">
</ul>
</div>
<div data-role="collapsible" data-collapsed="true">
<h2>Wesley</h2>
<ul id="wesleys" data-role="listview" data-filter="true" data-inset="true" data-icon="forbidden" data-autodividers="true">
</ul>
</div>
</div>
</div>
JavaScript:
$(document).ready( function() {
$.getJSON("assets/js/starTrek.json", function(data){
$.each(data.enterprise.humans, function(){
$("ul#humans").append('<li>' + '<a href="#' + this["url"] + '" data-transition="slide">'+ this["firstName"] + ' ' + this["lastName"] + '</a>' + '</li>');
$("ul#humans").listview("refresh");
});
$.each(data.enterprise.robots, function(){
$("ul#robots").append('<li>' + '<a href="#' + this["url"] + '" data-transition="slide">'+this["firstName"] + ' ' + this["lastName"] + '</a>' + '</li>');
$("ul#robots").listview("refresh");
});
$.each(data.enterprise.wesleys, function(){
$("ul#wesleys").append('<li>' + '<a href="#' + this["url"] + '" data-transition="slide">'+ this["firstName"] + ' ' + this["lastName"] + '</a>' + '</li>');
$("ul#wesleys").listview("refresh");
});
});
})
JSON:
{
"enterprise": {
"humans": [
{ "firstName":"Jean Luc" , "lastName":"Picard" , "url":"picard"},
{ "firstName":"Miles" , "lastName":"O'Brien" , "url":"obrien"},
{ "firstName":"Will" , "lastName":"Riker" , "url":"riker"}
{ "firstName":"Beverly" , "lastName":"Crusher" , "url":"crusher"},
{ "firstName":"Deanna" , "lastName":"Troi" , "url":"troi" },
{ "firstName":"Mr" , "lastName":"Worf" , "url":"worf"},
],
"robots": [
{ "firstName":"Data" , "lastName":" " , "url":"data"}
],
"wesleys": [
{ "firstName":"Wesley" , "lastName":"Crusher" , "url":"wesley"}
]
}
}