{"id":3162,"date":"2013-03-01T15:35:34","date_gmt":"2013-03-01T22:35:34","guid":{"rendered":"http:\/\/iohelix.net\/blog\/?p=3162"},"modified":"2014-01-03T10:09:59","modified_gmt":"2014-01-03T17:09:59","slug":"interesting-code-snippet","status":"publish","type":"post","link":"http:\/\/iohelix.net\/blog\/2013\/03\/interesting-code-snippet\/","title":{"rendered":"Interesting code snippet"},"content":{"rendered":"<p>I was looking into a function to allow me to sort an array of objects in JavaScript, and I stumbled upon this interesting little snippet:<\/p>\n<pre lang=\"javascript\"> &nbsp; &nbsp;[-1, 1][+!!reverse]<\/pre>\n<p>When I first looked at this, I wasn&#8217;t quite sure what it did, but as I started to take it apart, it became clear. It&#8217;s a very clever little piece of code.<\/p>\n<p>This was part of the return value of the sort function, which, if it returns a 1, sorts ASC, and -1 sorts DESC. This little snippet switches that.<\/p>\n<p>The way it works, is it creates a small two-element array <tt>[-1, 1]<\/tt> and then pulls out the n-th element where <tt>n<\/tt> is <tt>+!!reverse<\/tt>.<\/p>\n<p>It&#8217;s the same as writing:<\/p>\n<pre lang=\"javascript\"> &nbsp; &nbsp;var test = [-1, 1];\r\n &nbsp; &nbsp;var idx = +!!reverse;\r\n &nbsp; &nbsp;return test[idx];<\/pre>\n<p>The <tt>!!<\/tt> part makes sure that <tt>reverse<\/tt> is a boolean, and the <tt>+<\/tt> converts it to an int.<\/p>\n<p>Very interesting, and I had to look at it for a second to really figure it out.<\/p>\n<p>Original snippet location:<br \/>\n<a title=\"http:\/\/stackoverflow.com\/questions\/979256\/how-to-sort-an-array-of-javascript-objects#answer-979325\" href=\"http:\/\/stackoverflow.com\/questions\/979256\/how-to-sort-an-array-of-javascript-objects#answer-979325\">http:\/\/stackoverflow.com\/questions\/979256\/how-to-sort-an-array-of-javascript-objects#answer-979325<\/a><\/p>\n<p><strong>UPDATE:<\/strong> After playing with this code, and really looking at it, I&#8217;ve decided that this piece of code, while clever, is poor programming.<\/p>\n<p>The reason I think this is for one, it&#8217;s not clear what it does at first glance, and secondly, it can be re-written to be both more readable, and less prone to errors.<\/p>\n<p>I rewrote the code to look like this:<\/p>\n<pre lang=\"javascript\"> &nbsp; &nbsp;(reverse ? -1 : 1)<\/pre>\n<p>The reason I did this was because when I tried to format the previous bit of code to fit my coding style, it broke. Apparently, the <tt>+<\/tt> needs to be directly connected to the variable that follows it to get the JavaScript quirk of changing that variable to an integer.<\/p>\n<p>This small difference was enough for me to decide that this bit of code was too fragile and needed to go.<\/p>\n<p>If you&#8217;ll also notice, in the original code, <tt>reverse<\/tt> was actually set when the sort order was normal (returning <tt>1<\/tt>), and unset when the sort order was reversed (returning <tt>-1<\/tt>). &nbsp;I also fixed this, making the name of the variable match it&#8217;s function, thereby making the code even more clear.<\/p>","protected":false},"excerpt":{"rendered":"<p>I was looking into a function to allow me to sort an array of objects in JavaScript, and I stumbled upon this interesting little snippet: &nbsp; &nbsp;[-1, 1][+!!reverse] When I first looked at this, I wasn&#8217;t quite sure what it did, but as I started to take it apart, it became clear. It&#8217;s a very &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/iohelix.net\/blog\/2013\/03\/interesting-code-snippet\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Interesting code snippet&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[99],"tags":[],"class_list":["post-3162","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"http:\/\/iohelix.net\/blog\/wp-json\/wp\/v2\/posts\/3162"}],"collection":[{"href":"http:\/\/iohelix.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/iohelix.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/iohelix.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/iohelix.net\/blog\/wp-json\/wp\/v2\/comments?post=3162"}],"version-history":[{"count":0,"href":"http:\/\/iohelix.net\/blog\/wp-json\/wp\/v2\/posts\/3162\/revisions"}],"wp:attachment":[{"href":"http:\/\/iohelix.net\/blog\/wp-json\/wp\/v2\/media?parent=3162"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/iohelix.net\/blog\/wp-json\/wp\/v2\/categories?post=3162"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/iohelix.net\/blog\/wp-json\/wp\/v2\/tags?post=3162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}