JSort, your JavaScript library for sorting and reordering list or grid items.
Sortable CSS grid. Parent-drop and grab handler supported.
<div id="grid">
<div>One</div>
<div>
<div class="jsort-handler">Handler</div>
Two
</div>
</div>
<script type="module">
import JSort from "@rbuljan/jsort";
const elGrid = document.querySelector("#grid");
const jsortGrid = new JSort(elGrid, {/* Options */});
</script>
display: none;
Group multiple containers using the data-jsort attribute:
data-jsort="group: name"
<ul class="shared">
<li> A 1 </li>
<li> A 2 </li>
</ul>
<ul class="shared">
<li> B 1 </li>
</ul>
<script type="module">
import JSort from "@rbuljan/jsort";
document.querySelectorAll(".shared").forEach((el) => new JSort(el, { group: "shared" }));
</script>
Add a group: "name"
to Options, or use data-jsort="group: name"
to your nested elements.
<ul class="nested">
<li> A
<ul class="nested">
<li> B </li>
</ul>
</li>
</ul>
<script type="module">
import JSort from "@rbuljan/jsort";
document.querySelectorAll(".nested").forEach((el) => new JSort(el, { group: "nested" }));
</script>
Swap items between elements.
Add group: "name", swap: true
to Options, or use data-jsort="group: name; swap: true"
attribute to your grouped elements.
<ul class="swap" data-jsort="group:a;swap:true;">
<li> A 1 </li>
<li> A 2 </li>
</ul>
<ul class="swap" data-jsort="group:a;swap:true;">
<li> B 1 </li>
</ul>
<script type="module">
import JSort from "@rbuljan/jsort";
document.querySelectorAll(".swap").forEach((el) => new JSort(el));
</script>
What is an animated sortable library without a .sort()
method?
import JSort from "@rbuljan/jsort";
const myJSort = new JSort(document.querySelector(".jsort"));
// Your custom sorting logic:
const sortLogic = (a, b) => a.textContent.localeCompare(b.textContent, "en", { numeric: true });
// Sort:
myJSort.sort(sortLogic);
Here's what's asked the most
selectorIgnoreTarget
like: selectorIgnoreTarget: "p"
(to ignore paragraphs
descendants)jsort-ignore
to your desired items,
or modify the desired class in Options
selectorItemsIgnore: ".my-ignore-class"
(by default it's
".jsort-ignore"
)
:not()
CSS selector like:
selectorItems: "*:not(.ignore)"
and add that chstom .ignore
class to your Elements
".item"
and add it only to the
children
items you want to be draggable/sortablegrabTimeout
option to 0
on*
callback options, one being onDrop()
and see all the available `data` Object values.
myJSort(Element, {
onDrop(data) => {
console.log(data)
console.log(data.indexGrab, data.indexDrop);
console.log(this); // if you need even more info
});
});
MIT
© 2025 Roko C. Buljan