Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-61941

$sortArray comparator does not satisfy "Compare" requirement with -1 sort

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 5.3.0, 5.2.0-rc1
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None
    • Fully Compatible
    • ALL
    • v5.2
    • QE 2021-12-13
    • 162

      To sort an STL container with std::sort(), the custom comparator used must satisfy the Compare named requirement. Specifically,

      comp(a, b): Establishes strict weak ordering relation with the following properties:

      • For all a, comp(a,a)==false

      When sorting by whole values descending (specifically, sortBy: -1), we do the following negation:

          if (useWholeValue) {
              const bool ascending = ValueComparator(collator).getLessThan()(lhs, rhs);
      
              const bool reversed = (sortPattern.firstElement().number() < 0);
      
              return (reversed ? !ascending : ascending);
      

      Simply negating ascending is not correct, because when comparing two equal values, getLessThan() should return false for both.

      This logic error triggers a fatal assertion on Windows debug builds, because the MSVC STL implementation checks for the validity of the comparator.

            Assignee:
            andrii.dobroshynski@mongodb.com Andrii Dobroshynski (Inactive)
            Reporter:
            kyle.suarez@mongodb.com Kyle Suarez
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: