Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
</head>
<body>
  
  <table>
    <tr><th>Date</th></tr>
    <tr><td>2001-01-01</td></tr>
    <tr><td>2005-05-05</td></tr>
    <tr><td>2002-02-02</td></tr>
    <tr><td>2005-04-04</td></tr>
    <tr><td>2005-05-03</td></tr>
    
</body>
</html>
 
$('th').click(function(){
    var table = $(this).parents('table').eq(0)
    var rows = table.find('tr:gt(0)').toArray().sort(comparer($(this).index()))
    this.asc = !this.asc
    if (!this.asc){rows = rows.reverse()}
    for (var i = 0; i < rows.length; i++){table.append(rows[i])}
})
function comparer(index) {
    return function(a, b) {
        var valA = getCellValue(a, index), valB = getCellValue(b, index)
        return $.isNumeric(valA) && $.isNumeric(valB) ? valA - valB : valA.localeCompare(valB)
    }
}
function getCellValue(row, index){ return $(row).children('td').eq(index).html() }
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
nickgrealypro
0viewers