Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
New sorting method with Jade & Flexbox
//The slice, sort, and for loop are not necessary. However, they are there for your conveinence when adding new price keys. If you want to drop those things, then you would have to add and change a new "order" key & value each time you add a new price key & value. For example, in this demo, $245 is the highest price. You would want a priceAry.order value of 0 for it (or the corresponding CSS order property value). You can see how manual changes can become unmaintainable. I wanted to handle this side of things in the template and not on the client side JS.
- var priceAry = [{"price":"25"},{"price":"245"},{"price":"15"},{"price":"55"},{"price":"50"},{"price":"35"},{"price":"75"},{"price":"150"},{"price":"175"},{"price":"55"},{"price":"125"},{"price":"100"}]
- var newAry = priceAry.slice(0);
- priceAry.sort(function(a, b){
- return b.price-a.price
- })
- for (var i = 0; i < priceAry.length; i++) {
- priceAry[i].order = i
- }
button#default Arrange by relevance (default)
button#high-to-low Arrange highest to lowest
button#low-to-high Arrange lowest to highest
each val, index in newAry
li(class="sortable", data-order=val.order, data-default=index)
span.price= "$" + val.price

New sorting method with Jade & Flexbox

Generally, sorting is pretty easy. So is there a way to make it even easier? We can take advantage of CSS order to sort by 3 options. Here's the kicker: we don't need to use an array or object sort, and we don't need to use .append(). Most of the work is done in the Jade template. To watch it adapt to new data, add a new "price" key in the priceAry variable.

A Pen by Christopher Schuck on CodePen.


//Tested in Chrome, Firefox, & IE11
//No jQuery used. I wanted to keep it as fast and lightweight as possible
/*You are probably thinking to yourself, hey why not use row-reverse on the grid id? I tried that originally, but is was only reversing individual rows. If you wanted multiple rows then you were out of luck. This works even better.*/
function sortItems(dataType,neg) {
neg = neg || "";
var delay = 0;
var sortable = document.querySelectorAll(".sortable");
for (var i = 0; i < sortable.length; i++) {
var dataSelect = document.querySelector("[data-default='" + i + "']") = 0; = (delay += 0.02) + "s";
sortable[sortable.length-1].addEventListener("transitionend", function(){
for (var i = 0; i < sortable.length; i++) {
//Get the element by data-order and change the order style. That's all you have to do! You could probably substitute data-order for an id, but the data attribute seemed more logical
var dataSelect = document.querySelector("[data-" + dataType + "='" + i + "']") = neg + i; = 1; = (delay -= 0.02) + "s";
//reset & autoprefixed used
//I didn't use any transitions during the sort. I wanted to keep the concept extremely simple. Maybe in the future I'll add some cool transitions during the sort.
body, html {
height: 100%;
font-size: 100%
.button-wrapper {
display: flex;
flex-flow: row wrap;
justify-content: space-around;
background: lighten(lightblue,13%);
padding-bottom: 10px;
button {
font: 1.3rem 'Open Sans Condensed', sans-serif;
padding: 10px;
margin-top: 10px;
cursor: pointer;
border: none;
background: rgb(55,55,55);
color: #fff;
font-family: 'Open Sans Condensed', sans-serif;
transition: background 0.3s ease-in-out
button:hover {
background: rgb(105,105,105);
ul {
list-style: none;
min-height: 100%;
display: flex;
flex-flow: row wrap;
justify-content: space-around;
align-items: center;
background: lighten(lightblue,17%);
padding-top: 20px
.sortable {
display: flex;
flex-flow: column nowrap;
padding-bottom: 20px;
width: calc(25% - 40px);
transition: opacity 0.3s ease-in-out,
.hidden {
opacity: 0
.fake-image, .price {
width: 100%;
text-align: center;
background: #fff;
.fake-image {
font-size: 7rem;
color: darken(DarkGreen,5%);
padding-top: 10px
.price {
font: 1.7rem "Oswald", sans-serif;
color: rgb(75,75,75);
//Some simple media queries
@media only screen and (max-width: 768px) {
.sortable {
width: calc(50% - 20px)
@media only screen and (max-width: 480px) {
.sortable {
width: 100%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment