<!DOCTYPE html>
<html>
<body>
<div align="center">
<h2>Associative Sort using JSON</h2>
<p id="results"></p>
</div>
<script>
data = [];
data.push({"key":"2","value":"BBBB"});
data.push({"key":"1","value":"AAAA"});
// returns 0, 1 and -1
function sort1(a,b)
{
return (a.key == b.key ? 0 : (a.key > b.key ? 1 : -1 )); // by key ascending order
// return (a.value == b.value? 0 : (a.value > b.value? -1 : 1 )); // by value descending order
}
data.sort(sort1); // call the function sort
document.getElementById("results").innerHTML = JSON.stringify(data);
</script>
</body>
</html>