cource / MBITS Elive Attendance Sorter

// ==UserScript==
// @name         MBITS Elive Attendance Sorter
// @version      0.1
// @description  Automatically sort the attendance search page in Elive
// @author       Jeff Jacob Joy
// @match        */student/AttendanceSearch*
// @grant        none
// @license MIT
// ==/UserScript==

var parseDate = str =>
  new Date(str
    .match(/[0-9/]/g).join("")
    .split("/")
    .reverse()
  )

function getDateFromRow(row) {
  let data = row.getElementsByTagName("TD")[0].innerText || row.getElementsByTagName("TD")[1].innerText
  return parseDate(data)
}

(function sort() {
  'use strict';
  console.log("Sorting Attendance")

  let table = document.getElementById("ctl00_ContentPlaceHolder1_pnlAttendance").lastElementChild.lastElementChild
  let rows = table.rows
  let x, y
  for (var j = 0; j < (rows.length); j++) {
    for (var i = 1; i < (rows.length - 1 - j); i++) {
      x = getDateFromRow(rows[i])
      y = getDateFromRow(rows[i + 1])
      if (x > y) {
        table.firstElementChild.insertBefore(rows[i + 1], rows[i])
      }
    }
  }

})();