{"id":2554,"date":"2023-02-20T18:30:42","date_gmt":"2023-02-20T23:30:42","guid":{"rendered":"https:\/\/bitpost.com\/news\/?p=2554"},"modified":"2023-02-21T05:00:39","modified_gmt":"2023-02-21T10:00:39","slug":"note-to-self-javascript-sort-stupidness","status":"publish","type":"post","link":"https:\/\/bitpost.com\/news\/2023\/note-to-self-javascript-sort-stupidness\/","title":{"rendered":"NOTE TO SELF: JavaScript sort silliness"},"content":{"rendered":"\n<p>In c++, all a sort operation needs is a lessThan function.  Complex sorts are EASY:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct AnalyzerJobs_lessthan\n{\n    bool operator()(const AnalyzerJob* left, const AnalyzerJob* right) const\n    {\n        assert(left -&gt;psq_-&gt;second &amp;&amp; left -&gt;psq_-&gt;second-&gt;p_shared_aps_ &amp;&amp; left -&gt;psq_-&gt;second-&gt;p_shared_aps_-&gt;p_ad_);\n        assert(right-&gt;psq_-&gt;second &amp;&amp; right-&gt;psq_-&gt;second-&gt;p_shared_aps_ &amp;&amp; right-&gt;psq_-&gt;second-&gt;p_shared_aps_-&gt;p_ad_);\n\n        \/\/ Primary sort\n        \/\/ Critical jobs first\n        \/\/ If only one is critical, it wins.\n        if (left -&gt;psq_-&gt;second-&gt;p_shared_aps_-&gt;bCriticalAnalysisNeeded() != right-&gt;psq_-&gt;second-&gt;p_shared_aps_-&gt;bCriticalAnalysisNeeded())\n            return left -&gt;psq_-&gt;second-&gt;p_shared_aps_-&gt;bCriticalAnalysisNeeded();\n\n        \/\/ Secondary sort\n        \/\/ If only one has never been analyzed, it wins.\n        if (left-&gt;psq_-&gt;second-&gt;p_shared_aps_-&gt;bAnalyzed() != right-&gt;psq_-&gt;second-&gt;p_shared_aps_-&gt;bAnalyzed())\n            return right-&gt;psq_-&gt;second-&gt;p_shared_aps_-&gt;bAnalyzed();\n\n        \/\/ Tertiary sort by reverse timestamp of last analysis.\n        double lefttime  = left -&gt;psq_-&gt;second-&gt;p_shared_aps_-&gt;analysis_finished_timestamp_;\n        double righttime = right-&gt;psq_-&gt;second-&gt;p_shared_aps_-&gt;analysis_finished_timestamp_;\n        return (lefttime &gt; righttime);\n    }\n};<\/code><\/pre>\n\n\n\n<p>You&#8217;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&#8217;m assuming so they can eek out a bit of performance when sorting arrays that have a large number of &#8220;equal&#8221; items.  Seems an outlier case to optimize for, but it probably puts it on some slightly higher score on some manic benchmark.<\/p>\n\n\n\n<p>So for even the simplest sort, you have to do this stupidity:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>myArray.sort(( a, b ) =&gt; ( a.value &gt; b.value ? 1 : -1 ));<\/code><\/pre>\n\n\n\n<p>It seems to work as long as the sign is right, so this is also possible:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>myArray.sort(( a, b ) =&gt; ( a.value - b.value ));<\/code><\/pre>\n\n\n\n<p>Also, remember that JavaScript provides all kinds of &#8220;helpful&#8221; dangerous ugly <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/sort\" target=\"_blank\" rel=\"noopener\" title=\"\">default conversions<\/a>.  Cest la vie.  I have to remember though &#8211; I&#8217;ve used c++ style comparisons in JavaScript sort functions more than once, and that shit don&#8217;t fly.  ok byeeeee!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In c++, all a sort operation needs is a lessThan function. Complex sorts are EASY: You&#8217;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&#8217;m assuming so they can eek out a bit of performance [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[22,2],"tags":[],"class_list":["post-2554","post","type-post","status-publish","format-standard","hentry","category-cpp","category-chatter"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9M11L-Fc","jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/posts\/2554","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/comments?post=2554"}],"version-history":[{"count":2,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/posts\/2554\/revisions"}],"predecessor-version":[{"id":2559,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/posts\/2554\/revisions\/2559"}],"wp:attachment":[{"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/media?parent=2554"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/categories?post=2554"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/tags?post=2554"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}