Sort by, then by
My favorite sorting algorithm.
HTML
<table id="t1"></table>
<table id="t2"></table>
<table id="t3"></table>
<table id="t4"></table>
CSS
html { font: normal 62.5% Arial, sans-serif; }
body { padding: 10px; }
table {
font-size: 1.3em;
margin-bottom: 18px;
width: 100%;
border: none;
}
table caption {
font-weight: bold;
font-size: 1.5rem;
}
table td {
color: #999;
padding: 9px;
vertical-align: middle;
text-align: left;
line-height: 1em;
border-bottom: 1px solid #bcbec0;
}
table .number {
text-align: center;
}
.sortby {
color: #333;
font-weight: bold;
}
.thenby {
color: #333;
}
JavaScript
/* "Sort by, then by"
// Based on this jsfiddle:
// http://jsfiddle.net/dFNva/1/
// as a response to this stackoverflow post:
// http://stackoverflow.com/a/979325/2502532
// Improvements include JSON deep access with path notation, 'then by' sorting.
// Description: Sort by object key, with optional reverse ordering, priming, and 'then by' sorting.
array.sort(
by(path[, reverse[, primer[, then]]])
);
*/
/* THE FUNCTION */
var by = function (path, reverse, primer, then) {
var get = function (obj, path) {
if (path) {
path = path.split('.');
for (var i = 0, len = path.length - 1; i < len; i++) {
obj = obj[path[i]];
};
return obj[path[len]];
}
return obj;
},
prime = function (obj) {
return primer ? primer(get(obj, path)) : get(obj, path);
};
return function (a, b) {
var A = prime(a),
B = prime(b);
return (
(A < B) ? -1 :
(A > B) ? 1 :
(typeof then === 'function') ? then(a, b) : 0
) * [1,-1][+!!reverse];
};
};
/* THE ARRAY */
var places = [
{
"publication_id": "07e1fccd-a3a2-4025-9650-f3ef2dd6d874",
"country": "US",
"platform_page_id": "1757782394471400",
"platform_post_url": "https://www.facebook.com/brutofficiel/videos/1985920284990942/",
"description": "FILTRE",
"platform_page": "brutofficiel",
"title": "Filtre Anglais ",
"published_at": "2018-03-12T14:39Z",
"platform": "facebook",
"platform_post_id": "1985920284990942",
"media": {
"source": [],
"journalists": [],
"archives": [],
"interviewees": [],
"tags": [],
...