Sort objects in array by property (Vanilla)

/** * Sort function that sorts an array of objects by comparing a property within each object * @param {String} property Property * @returns {function} Sorted */ function dynamicSort(property) { var sortOrder = 1; if (property[0] === "-") { sortOrder = -1; property = property.substr(1); } return function (a, b) { var result = a[property] < b[property] ? -1 : a[property] > b[property] ? 1 : 0; return result * sortOrder; }; }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.