D3 sorts an array of objects by 2 object properties

I have a dataset with an array of objects containing 4 properties of an object, for example:

var dataset = [{
    key: 0,
    brand: "A",
    value: 10,
    size: 10
}, {
    key: 1,
    brand: "B",
    value: 11,
    size: 10
}, {
    key: 2,
    brand: "C",
    value: 12,
    size: 9
}, {
    key: 3,
    brand: "D",
    value: 13,
    size: 9
}, {
    key: 4,
    brand: "E",
    value: 14,
    size: 9
}, {
    key: 5,
    brand: "G",
    value: 15,
    size: 9
}, {
    key: 6,
    brand: "F",
    value: 16,
    size: 9
}, {
    key: 7,
    brand: "H",
    value: 17,
    size: 9
}, {
    key: 8,
    brand: "I",
    value: 18,
    size: 9
}, {
    key: 9,
    brand: "J",
    value: 19,
    size: 9
}, {
    key: 10,
    brand: "K",
    value: 20,
    size: 9
}];

Then I create a graph with D3 showing valueas horizontal lines, and sizeas bubbles sitting on top of the lines (see http://jsfiddle.net/G74Aq/4/ ).

, key, . , , -. , , 10, , , , , 0 < 10 ( ).

: -, ? , , ?

+3
1

. + .

function getSortFunction(propertyName) {
    return function(a, b) {
        return +(a[propertyName]) > +(b[propertyName]);
    }
}

dataset.sort(getSortFunction("size"));
+1

All Articles