Custom Sort Function

XMLWordPrintableJSON

    • Type: New Feature
    • Resolution: Duplicate
    • Priority: Major - P3
    • None
    • Affects Version/s: 2.0.0
    • Component/s: JavaScript, Querying
    • None
    • None
    • 3
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      Similar concept to SERVER-4079. Also related to the needs behind SERVER-458.

      Allow a custom compare function for sorting.

      db.people.find().sort((_foo_bar_baz: function(a, b) {return (a.foo + a.bar + a.baz) - (b.foo + b.bar + b.baz);}})

      This might also make sense (in addition to the previous syntax, not instead).
      db.people.find().sort(function(a, b)

      {return (a.foo + a.bar + a.baz) - (b.foo + b.bar + b.baz);}

      )

      I keep wanting this. One case that keeps popping up is sorting tabular data. Often times columns are computed from the contents of a couple of fields and the best sort order in the mind of the user will not actually have a simple relationship to any of the stored fields. The current workaround is to store pre-computed fields where possible, and disable sorting where this is not possible (or not practical), but precomputed fields are often not a good option (especially when the sort is rare).

              Assignee:
              Unassigned
              Reporter:
              John Crenshaw
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

                Created:
                Updated:
                Resolved: