       a = @_colorGradientData[idx+3]
       return "rgba(#{r},#{g},#{b},#{a/255.0})"
 
+    _sortData: ->
+      @_data.sort (a,b) ->
+        return a[2] - b[2]
+
     _redraw: ->
       gridSize = @options?.gridSize || @defaultGridSize
       size = @_map.getSize()

      ctx.mozImageSmoothingEnabled = false
      ctx.clearRect(0, 0, @_canvas.width, @_canvas.height)

      for latLng in @_data
        continue if not bounds.contains(latLng)
        p = @_map.latLngToContainerPoint(latLng)
        ctx.fillStyle = @_getColor(latLng[2])
        ctx.fillRect(p.x-gridSize/2, p.y-gridSize/2, gridSize, gridSize)
        # ctx.drawImage(drawRect, p.x-gridSize/2, p.y-gridSize/2)

      @_frame = null


    initialize: (options, data=[])->
      @_data = data
      @_sortData()
      L.setOptions(this, options)

    setData: (data=[]) ->
      @_data = data
      @_sortData()
      @redraw()

    redraw: ->
