{"id":3126,"date":"2016-02-10T00:44:09","date_gmt":"2016-02-10T00:44:09","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=3126"},"modified":"2016-02-10T00:44:09","modified_gmt":"2016-02-10T00:44:09","slug":"scala-sortwith-example","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/scala-sortwith-example\/","title":{"rendered":"Scala sortWith example"},"content":{"rendered":"<p>The &#8220;sortWith&#8221; function lets you sort an array according to a specified rule.<\/p>\n<p>To demonstrate this, let&#8217;s make a list of random numbers:<\/p>\n<pre lang=\"scala\">\nval x = \n  List.fill(20)(100)\n      .map(scala.util.Random.nextInt)\n\nx: List[Int] = \n  List(90, 13, 69, 46, 66, \n       86, 38, 18, 88, 26, \n       81, 18, 11, 74, 5, \n       50, 7, 42, 74, 57)\n<\/pre>\n<p>You can sort this like so:<\/p>\n<pre lang=\"scala\">\nx.sortWith(\n  (a, b) => {\n    a > b\n  }\n)\n<\/pre>\n<p>Note that you should never use &#8220;return&#8221; in scala. <\/p>\n<p>The above code can be re-written more simply:<\/p>\n<pre lang=\"scala\">\nx.sortWith(\n  (a, b) =>\n    a > b\n)\n<\/pre>\n<p>However, &#8220;a&#8221;, and &#8220;b&#8221; have no importance here. For simple implementations, you can use an underscore syntax, <\/p>\n<pre lang=\"scala\">\nx.sortWith(_ > _)\n<\/pre>\n<p>What this does is to create a comparison function that takes two arguments, and checks if the first is larger than the second &#8211; in this case the &#8220;_&#8221; is not a variable, but more of a placeholder syntax.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The &#8220;sortWith&#8221; function lets you sort an array according to a specified rule. To demonstrate this, let&#8217;s make a list of random numbers: val x = List.fill(20)(100) .map(scala.util.Random.nextInt) x: List[Int] = List(90, 13, 69, 46, 66, 86, 38, 18, 88, 26, 81, 18, 11, 74, 5, 50, 7, 42, 74, 57) You can sort this &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/scala-sortwith-example\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Scala sortWith example&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[480,482,485],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3126"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=3126"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3126\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=3126"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=3126"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=3126"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}