Skip to content

Instantly share code, notes, and snippets.

@hden
Created January 11, 2013 03:47
Show Gist options
  • Select an option

  • Save hden/4507809 to your computer and use it in GitHub Desktop.

Select an option

Save hden/4507809 to your computer and use it in GitHub Desktop.
Simple concept of cascade sorting
_ = require 'underscore'
m = [[0, 0, 1, 1]
[1, 1, 0, 0]
[0, 1, 0, 1]
[0, 1, 1, 1]
[1, 0, 1, 0]
[1, 0, 1, 0]]
step = 0
notIdentical = (matrix) ->
uniq = _.uniq matrix, false, (item) -> item.toString()
return if uniq? then true else false
check = (value) ->
step++
console.log "step: #{step}"
console.log value
iterator = (n) ->
if n is 1 then true else false
sort = (matrix, column=0, isUpward=true, callback) ->
length = matrix.length
res = matrix
if length > 1 and column < matrix[0].length and notIdentical matrix
res = []
hit = _.filter matrix, (row) -> iterator row[column]
miss = _.filter matrix, (row) -> not iterator row[column]
next = column + 1
hit = sort hit, next, false
miss = sort miss, next, true
# set isUpward = true for conventional cascade sorting
isUpward = true
[foo, bar] = if isUpward then [hit, miss] else [miss, hit]
res = []
res.push item for item in foo if foo?
res.push item for item in bar if bar?
callback res if callback?
# clean up
hit = miss = next = foo = bar = null
return res
k = sort m
console.log 'Result:'
console.log k
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment