Skip to content

Commit

Permalink
Implement @haferje 's Date parse/sort support in coffee script HubSpot#9
Browse files Browse the repository at this point in the history
  • Loading branch information
adamschwartz committed Feb 20, 2014
1 parent 4092e4f commit f9303b4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
16 changes: 15 additions & 1 deletion coffee/sortable.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ sortable =
getColumnType: (table, i) ->
for row in table.tBodies[0].rows
text = sortable.getNodeValue row.cells[i]
return sortable.types.numeric if text isnt '' and text.match(numberRegExp)
if text isnt ''
if text.match(numberRegExp)
return sortable.types.numeric
if not isNaN Date.parse(text)
return sortable.types.date

return sortable.types.alpha

getNodeValue: (node) ->
Expand All @@ -94,6 +99,15 @@ sortable =
compare: (a, b) ->
a[0].localeCompare b[0]

date:
defaultSortDirection: 'ascending'
compare: (a, b) ->
aa = Date.parse(a[0])
bb = Date.parse(b[0])
aa = 0 if isNaN(aa)
bb = 0 if isNaN(bb)
aa - bb

setTimeout sortable.init, 0

window.Sortable = sortable
24 changes: 22 additions & 2 deletions js/sortable.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f9303b4

Please sign in to comment.