sort array on object value

sorts and array on a "key"

by bob

HTML

<div id="d1"></div>
<div id="d2"></div>
<div id="d3"></div>
<div id="d4"></div>
<div id="d5"></div>
<div id="d6"></div>
<div id="d7"></div>
<div id="d8"></div>
<div id="d9"></div>
<div id="d10"></div>

CSS

body{
	padding: 10px;
}
table tr td {
	padding: 0px;
	font-size: 12px;
	line-height: 14px;
	height: 10px;
	width: auto;
	margin: 0px;
	font-family: Verdana, Geneva, sans-serif;
}
 p {
	font-size: 12px;
	font-weight: bold;
	font-family: Verdana, Geneva, sans-serif;
}
.hot {
	color: #F00;
}
.cold {
	color: #000;
}

JavaScript

var homes = [{
    "id": "3",
    "city": "Dallas",
    "state": "TX",
    "zip": "75201",
    "price": "$430,876.99"
}, {
    "id": "4",
    "city": "Bevery Hills",
    "state": "CA",
    "zip": "90210",
    "price": "$1,120,256.00"
}, {
    "id": "5",
    "city": "New York",
    "state": "NY",
    "zip": "00010",
    "price": "700256.00"
}];

var sortOn = function (arr, prop, reverse, numeric) {

    // Ensure there's a property
    if (!prop || !arr) {
        return arr
    }

    // Set up sort function
    var sort_by = function (field, rev, primer) {

        // Return the required a,b function
        return function (a, b) {

            // Reset a, b to the field
            a = primer(a[field]), b = primer(b[field]);

            // Do actual sorting, reverse as needed
            return ((a < b) ? -1 : ((a > b) ? 1 : 0)) * (rev ? -1 : 1);
        }

    }

    // Distinguish between numeric and string to prevent 100's from coming before smaller
    // e.g.
    // 1
    // 20
    // 3
    // 4000
    // 50

    if (numeric) {

        // Do sort "in place" with sort_by function
        arr.sort(sort_by(prop, reverse, function (a) {

            // - Force value to a string.
            // - Replace any non numeric characters.
            // - Parse as float to allow 0.02 values.
            return parseFloat(String(a).replace(/[^0-9.-]+/g, ''));

        }));
    } else {

        // Do sort "in place" with sort_by function
        arr.sort(sort_by(prop, reverse, function (a) {

            // - Force value to string.
            return String(a).toUpperCase();

        }));
    }


}

function stringResult(elemID, doing, label) {
    var elem = document.getElementById(elemID);
    var result = "<p>" + label + "</p>";   
    result += '<table cellpadding="10" cellspacing="5">';
    
    for (var i = 0; i < homes.length; i++) {
        result += "<tr>";
        result += '<td class="' + (doing == "id" ? "hot" : "cold") + '">';
        result +=...