 window.sorted_proposals = (proposals) ->
 
   sort = fetch 'sort_proposals'
-  set_sort() if !sort.func? 
-
-
-  if sort.comp
-    comp = sort.comp
-    proposals = proposals.slice().sort (a,b) ->
-      return comp(a,b) 
-
-  else 
-    proposal_rank = sort.func
-    proposals = proposals.slice().sort (a,b) ->
-      return proposal_rank(b, sort.opinion_value) - proposal_rank(a, sort.opinion_value)
+
+  set_sort() if !sort.name? 
+
+  comp = sort.comp
+  proposals = proposals.slice().sort comp
 
   # filter out filtered proposals
   filter_out = fetch 'filtered'

      @local.sort_menu = false
      save @local


    SPAN
      style: 
        color: 'black'
        fontSize: 14

      "sort by "


      DIV 
        ref: 'menu_wrap'



sort_options = [

  { 
    comp: (a, b) -> basic_proposal_scoring(b, ((o) -> o.stance)).avg - basic_proposal_scoring(a, ((o) -> o.stance)).avg
    name: 'Average Score'
    description: "Each proposal is scored by the average opinion score, where opinions are on [-1, 1]."
  }, { 
    comp: (a, b) -> basic_proposal_scoring(b, ((o) -> o.stance)).sum - basic_proposal_scoring(a, ((o) -> o.stance)).sum
    name: 'Total Score'
    description: "Each proposal is scored by the sum of opinions, where opinions are on [-1, 1]."
  }, {
    name: 'Trending'
    description: "'Total Score', except that newer opinions and topics are weighed more heavily."

    comp: (a, b) ->
      ov = (o) -> 
        ot = new Date(o.updated_at).getTime()
        n = Date.now()
        o.stance / (1 + Math.pow((n - ot) / 100000, 2))

      val = (proposal) -> 
        sum = basic_proposal_scoring(proposal, ov).sum
        n = Date.now()
        pt = new Date(proposal.created_at).getTime()
        sum / (1 + (n - pt) / 10000000000)  # decrease this constant to favor newer proposals

      val(b) - val(a)
  },
  { 
    name: 'Alphabetically'
    comp: (a, b) -> a.name.localeCompare b.name
    description: "Sort alphabetically by the proposal's title"
  }, {
    comp: (a,b) -> new Date(b.created_at).getTime() - new Date(a.created_at).getTime()
    name: 'Newest'
    description: "The proposals submitted most recently are shown first."
  }, { 
    name: 'Unity'
    description: "Proposals where the community is most united for or against is shown highest."
    comp: (a,b) -> 
      ov = (o) -> o.stance
      val = (proposal) ->
        stats = basic_proposal_scoring(proposal, ov)
        if stats.opinions.length > 1
          Math.log(stats.opinions.length + 1) / stats.std_dev / (1 - Math.abs(stats.avg) + 1)
        else 
          -1
      val(b) - val(a)

  }, { 
    name: 'Difference'
    description: "Proposals where the community is most split is shown highest."
    comp: (a,b) -> 
      ov = (o) -> o.stance
      val = (proposal) ->
        stats = basic_proposal_scoring(proposal, ov)
        if stats.opinions.length > 1
          stats.std_dev * Math.log(stats.opinions.length + 1) 
        else
          -1
      val(b) - val(a)

  }, {
    name: 'Most Activity'
    description: "Ranked by number of opinions and discussion."

    comp: (a,b) ->
      ov = (o) -> 1 + (o.point_inclusions or []).length
      basic_proposal_scoring(b, ov).sum - basic_proposal_scoring(a, ov).sum

  }


]

set_sort = -> 
  sort = fetch 'sort_proposals'
  if !sort.name?
    found = false 
    loc = fetch('location')
    if loc.query_params?.sort_by
