Skip to content

Instantly share code, notes, and snippets.

@leepfrog
Created March 20, 2013 19:23
Show Gist options
  • Select an option

  • Save leepfrog/5207638 to your computer and use it in GitHub Desktop.

Select an option

Save leepfrog/5207638 to your computer and use it in GitHub Desktop.
SortPipe
Blocks.SortPipePlugin = Em.PipePlugin.extend
# Define a list of sorting properties
observes: Em.computed.alias('sortProperties')
# The sorting function to use
sortFunction: Em.compare
# Our sort routine
process: (inputArr) ->
# DS.ManyArray does not support sort()
inputArr = inputArr.toArray() if inputArr.constructor == DS.ManyArray
inputArr.sort (obj1, obj2) =>
retVal = 0
props = @get('observes').slice(0)
while retVal == 0 && props.length > 0
sortProperty = props.shift()
retVal = @get('sortFunction')( obj1.get(sortProperty), obj2.get(sortProperty), sortProperty)
return retVal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment