In c++, all a sort operation needs is a lessThan function. Complex sorts are EASY:

struct AnalyzerJobs_lessthan
{
    bool operator()(const AnalyzerJob* left, const AnalyzerJob* right) const
    {
        assert(left ->psq_->second && left ->psq_->second->p_shared_aps_ && left ->psq_->second->p_shared_aps_->p_ad_);
        assert(right->psq_->second && right->psq_->second->p_shared_aps_ && right->psq_->second->p_shared_aps_->p_ad_);

        // Primary sort
        // Critical jobs first
        // If only one is critical, it wins.
        if (left ->psq_->second->p_shared_aps_->bCriticalAnalysisNeeded() != right->psq_->second->p_shared_aps_->bCriticalAnalysisNeeded())
            return left ->psq_->second->p_shared_aps_->bCriticalAnalysisNeeded();

        // Secondary sort
        // If only one has never been analyzed, it wins.
        if (left->psq_->second->p_shared_aps_->bAnalyzed() != right->psq_->second->p_shared_aps_->bAnalyzed())
            return right->psq_->second->p_shared_aps_->bAnalyzed();

        // Tertiary sort by reverse timestamp of last analysis.
        double lefttime  = left ->psq_->second->p_shared_aps_->analysis_finished_timestamp_;
        double righttime = right->psq_->second->p_shared_aps_->analysis_finished_timestamp_;
        return (lefttime > righttime);
    }
};

You’d think JavaScript would keep that battle-tested methodology, but they decided it would be better to expect a result from the sort function of [ -1, 0, 1 ]. I’m assuming so they can eek out a bit of performance when sorting arrays that have a large number of “equal” items. Seems an outlier case to optimize for, but it probably puts it on some slightly higher score on some manic benchmark.

So for even the simplest sort, you have to do this stupidity:

myArray.sort(( a, b ) => ( a.value > b.value ? 1 : -1 ));

It seems to work as long as the sign is right, so this is also possible:

myArray.sort(( a, b ) => ( a.value - b.value ));

Also, remember that JavaScript provides all kinds of “helpful” dangerous ugly default conversions. Cest la vie. I have to remember though – I’ve used c++ style comparisons in JavaScript sort functions more than once, and that shit don’t fly. ok byeeeee!

Leave a Reply