Skip to content

Instantly share code, notes, and snippets.

@phlco
Last active August 25, 2016 23:14
Show Gist options
  • Save phlco/b750448fa7ae3e4474546d6613ced4d3 to your computer and use it in GitHub Desktop.
Save phlco/b750448fa7ae3e4474546d6613ced4d3 to your computer and use it in GitHub Desktop.

Outcomes Tracker Sort by Last Name

function sort() {
  var names = Array.from( document.querySelectorAll('.student-name') );
  names.sort(function(a, b) {
    var a = a.textContent.trim().split(' ')[1];
    var b = b.textContent.trim().split(' ')[1];
    return a == b ? 0 : (a < b ? -1 : 1);
  });
  var list = document.querySelector('.outcomes-table');
  names.forEach(function(name){
    var tag = name;
    while( !tag.parentNode.classList.contains('row') ){
      tag = tag.parentNode;
    }
    tag = tag.parentNode;
    list.appendChild(tag);
  });
}
sort();

Bookmarklet

<a href="javascript:(function()%7Bfunction%20sort()%20%7Bvar%20names%20%3D%20Array.from(%20document.querySelectorAll('.student-name')%20)%3Bnames.sort(function(a%2C%20b)%20%7Bvar%20a%20%3D%20a.textContent.trim().split('%20')%5B1%5D%3Bvar%20b%20%3D%20b.textContent.trim().split('%20')%5B1%5D%3Breturn%20a%20%3D%3D%20b%20%3F%200%20%3A%20(a%20%3C%20b%20%3F%20-1%20%3A%201)%3B%7D)%3Bvar%20list%20%3D%20document.querySelector('.outcomes-table')%3Bnames.forEach(function(name)%7Bvar%20tag%20%3D%20name%3Bwhile(%20!tag.parentNode.classList.contains('row')%20)%7Btag%20%3D%20tag.parentNode%3B%7Dtag%20%3D%20tag.parentNode%3Blist.appendChild(tag)%3B%7D)%3B%7Dsort()%7D)()">Sort by Last Name</a>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment