         profits_per_unit: values.profits_per_unit
         label: @series_labels[key] || key
 
-
     sorted_items = items.sort(@sorting_function)
-
-    data = {series: sorted_items}
-
-    $('#merit-order-table').html tmpl(data)
+    $('#merit-order-table').html tmpl({series: sorted_items})
     true
 
+  # Sort by two fields
+  #
   sorting_function: (a,b) =>
     pa = @profitability_index a
     pb = @profitability_index b


  dashboard_value: =>
    return null unless @app.settings.merit_order_enabled()
    q = @gquery.future_value()
    profitables = _.select(_.values(q), (v) -> v.profitable == 'profitable' ).length
    tot = _.keys(q).length
    profitables/tot

  format_table: =>
    tmpl = _.template $('#merit-order-table-template').html()

    if pa != pb
      return -1 if pa < pb
      return 1 if pa > pb
    # sort by descending profits
    return -1 if ca > cb
    return 1 if ca < cb
    return 0

  # Index used for sorting
  #
  profitability_index: (x) ->
    switch x.profitable
      when 'profitable' then 0
      when 'conditionally_profitable' then 1
      when 'unprofitable' then 2

  # This hash maps dispatchable keys to the chart series labels used by the
  # other merit order table
  #
  series_labels:
    energy_chp_combined_cycle_network_gas: 'central_gas_chp'
    energy_chp_ultra_supercritical_coal: 'coal_chp'
