// 201806: removed JQuery dependency
((d) => {
const utils = init();
d.addEventListener("click", utils.doSort);
utils.numberRows(1);
d.querySelector("table tr th:first-child").click();
function sortColumn(evt) {
const origin = utils.findParentEl(evt.target, "th");
const columnIndex = origin && parseInt(origin.cellIndex);
const sortingOn = origin && origin.dataset.sortable || null;
return !origin || isNaN(columnIndex) || !sortingOn ? true : doSort();
function doSort() {
const forTable = utils.findParentEl(origin, "table") || null;
const currentDirection = utils.getCurrentDirection(origin);
const cells2Sort = Array.from(forTable.querySelectorAll(`tr td:nth-child(${
columnIndex + 1})`)) || null;
// are all value types in column the right type?
if (!utils.checkColumnValueTypes(cells2Sort, sortingOn)) {
origin.dataset.sortable = null;
return alert(`Sorry, one or more of the values in this column are not of type [${
sortingOn}]`);
}
// set current column properties
Array.from(d.querySelectorAll("[data-sortable]")).forEach(utils.redoHeaders);
origin.classList.add("sortedhere");
// Note: this will take effect in the *next* sort if applicable
// should happen before cloning
origin.dataset.direction = utils.toggleDirection(currentDirection);
let tableClone = forTable.cloneNode(true);
let cloneRows = Array.from(tableClone.rows).slice(1);
// No rows? do nothing
if (!cloneRows.length) {
return;
}
// extract column values and create sortable array
let sortVals = utils.getSortValues(cloneRows, columnIndex, sortingOn);
// assign sorting parameter and sort using factory method [sortHelper]
sortVals = sortVals.sort(utils.sorter({
key: "value",
[sortingOn]: 1
}, currentDirection));
// reorder tablerows cf sorting
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.