Sort Elements of Array in Alphabetical Order

Self-Innovator
Posted by Self-Innovator under jQuery category on | Points: 40 | Views : 1967
Using jQuery Sort the names of an arry in Alphabetical Order

<form id="form1" runat="server">
<div id="main1">
</div>
<div id="main2">
</div>
</form>


jQuery Script Code
$(document).ready(function() {
var members = [
{ "name": "CM Punk", "id": 003, "email": "punk@wwewreslt.com" },
{ "name": "The Mizery", "id": 004, "email": "miz@awesome.com" },
{ "name": "John Lunaritics", "id": 001, "email": "lunaritics@hotmailcom" },
{ "name": "Jessica", "id": 002, "email": "Jessie@gmailcom" },
{ "name": "Salman", "id": 005, "email": "sallubhwanted@bollywoodstar.com" }
];

$.each(members, function(index, value) {
$("#main1").append("<p><strong>Id:</strong>" + value.id + "- <strong>Name:</strong>" + value.name + "- <strong>Email:</strong>" + value.email + "</p>");
});
members = members.sort(function(a, b) {
if (a.name < b.name) { return -1 };
if (a.name > b.name) { return 1 };
return 0;
});
$.each(members, function(index, value) {
$("#main2").append("<p><strong>Id:</strong>" + value.id + "- <strong>Name:</strong>" + value.name + "- <strong>Email:</strong>" + value.email + "</p>");
});
});

Comments or Responses

Login to post response