var people = [{
"f_name": "john",
"l_name": "doe",
"sequence": 0
}, {
"f_name": "michael",
"l_name": "Goodyear",
"sequence": 1
}, {
"f_name": "bill",
"l_name": "Johnson",
"sequence": 4
}, {
"f_name": "will",
"l_name": "malone",
"sequence": 2
}, {
"f_name": "tim",
"l_name": "Allen",
"sequence": 3
}];
function sortJsonLcase(element, prop, asc) {
element = element.sort(function(a, b) {
if (asc) {
return (a[prop] > b[prop]) ? 1 : ((a[prop] < b[prop]) ? -1 : 0);
} else {
return (b[prop] > a[prop]) ? 1 : ((b[prop] < a[prop]) ? -1 : 0);
}
});
}
function sortJson(element, prop, propType, asc) {
switch (propType) {
case "int":
element = element.sort(function(a, b) {
if (asc) {
return (parseInt(a[prop]) > parseInt(b[prop])) ? 1 : ((parseInt(a[prop]) < parseInt(b[prop])) ? -1 : 0);
} else {
return (parseInt(b[prop]) > parseInt(a[prop])) ? 1 : ((parseInt(b[prop]) < parseInt(a[prop])) ? -1 : 0);
}
});
break;
default:
element = element.sort(function(a, b) {
if (asc) {
return (a[prop].toLowerCase() > b[prop].toLowerCase()) ? 1 : ((a[prop].toLowerCase() < b[prop].toLowerCase()) ? -1 : 0);
} else {
return (b[prop].toLowerCase() > a[prop].toLowerCase()) ? 1 : ((b[prop].toLowerCase() < a[prop].toLowerCase()) ? -1 : 0);
}
});
}
}
function sortJsonString() {
sortJson(people, 'l_name', 'string', $("#chkAscString").prop("checked"));
display();
}
function sortJsonInt() {
sortJson(people, 'sequence', 'int', $("#chkAscInt").prop("checked"));
display();
}
function sortJsonUL() {
sortJsonLcase(people, 'l_name', $('#chkAsc').prop('checked'));
display();
}
function display() {
$("#data").empty();
$(people).each(function() {
$("#data").append("<div class='people'>" + this.l_name + "</div><div class='people'>" + this.f_name + "</div><div class='people'>" + this.sequence + "</div><br />");
});
}
body {
font-family: Arial;
}
.people {
display: inline-block;
width: 100px;
border: 1px dotted black;
padding: 5px;
margin: 5px;
}
.buttons {
border: 1px solid black;
padding: 5px;
margin: 5px;
float: left;
width: 20%;
}
ul {
margin: 5px 0px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="buttons" style="background-color: rgba(240, 255, 189, 1);">
Sort the JSON array <strong style="color: red;">with</strong> toLowerCase:
<ul>
<li>Type: string</li>
<li>Property: lastname</li>
</ul>
<button onclick="sortJsonString(); return false;">Sort JSON</button>
Asc Sort
<input id="chkAscString" type="checkbox" checked="checked" />
</div>
<div class="buttons" style="background-color: rgba(255, 214, 215, 1);">
Sort the JSON array <strong style="color: red;">without</strong> toLowerCase:
<ul>
<li>Type: string</li>
<li>Property: lastname</li>
</ul>
<button onclick="sortJsonUL(); return false;">Sort JSON</button>
Asc Sort
<input id="chkAsc" type="checkbox" checked="checked" />
</div>
<div class="buttons" style="background-color: rgba(240, 255, 189, 1);">
Sort the JSON array:
<ul>
<li>Type: int</li>
<li>Property: sequence</li>
</ul>
<button onclick="sortJsonInt(); return false;">Sort JSON</button>
Asc Sort
<input id="chkAscInt" type="checkbox" checked="checked" />
</div>
<br />
<br />
<div id="data" style="float: left; border: 1px solid black; width: 60%; margin: 5px;">Data</div>