{"id":79874,"date":"2024-03-23T13:56:24","date_gmt":"2024-03-23T04:56:24","guid":{"rendered":"https://freelance.indieverse.co.jp/media/?p=79874"},"modified":"2026-10-07T10:58:58","modified_gmt":"2026-10-07T01:58:58","slug":"ruby-sort","status":"publish","type":"post","link":"https://freelance.indieverse.co.jp/media/programming/ruby/ruby-sort","title":{"rendered":"Rubyのsortメソッドの使い方 | sort_byとの違いも解説"},"content":{"rendered":"<p>この記事では、</p>\n<ul></ul>\n<ul>\n<li>Rubyでの基本的なSortメソッドの使い方</li>\n<li>配列やオブジェクトを含む複雑なデータ構造のソート方法</li>\n<li>SortとSort_byメソッドを使い分けるコツ</li>\n</ul>\n<p>について現役Rubyエンジニアが解説していきたいと思います。</p>\n<p><strong>著者：河合大</strong></p>\n<div class='balloon5'>\n<div class='faceicon'>\n\t\t\t<img src='https://pbs.twimg.com/profile_images/1230103371664613376/PHLMWlPU_400x400.jpg'></p>\n<div style='text-align:center'>DAI</div>\n</p></div>\n<div class='chatting'>\n<div class='says'>\n<a href=\"https://www.worksap.co.jp/\">株式会社ワークスアプリケーションズ</a>にてQAエンジニア→<a href=\"https://aidemy.co.jp/\">株式会社Aidemy</a>にてAIプログラミングスクールのマーケティングを担当→キャリアコーチングサービスのパイオニアである<a href=\"https://www.posiwill.co.jp/\">ポジウィル株式会社</a>にCMO就任→<a href=\"https://indieverse.jp/\">株式会社インディバース</a>を創業。<br />\nIT系キャリアに関して情報発信している<a href=\"https://freelance.indieverse.co.jp/media/\">Indieverse Freelance</a>を運営。自身も自社プロダクトである<a href=\"https://media-analytics.jp/\">Media Analytics</a>の開発をRuby on Railsで行うWebエンジニアである。本業はWebマーケター。最近は生成AI系の開発にどハマり中。著書は<a href=\"https://www.amazon.co.jp/%E7%8B%AC%E5%AD%A6%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9E%E3%83%BC%E3%81%AE%E3%81%9F%E3%82%81%E3%81%AEAI%E3%82%A2%E3%83%97%E3%83%AA%E9%96%8B%E7%99%BA%E3%81%8C%E3%82%8F%E3%81%8B%E3%82%8B%E6%9C%AC-%E6%B2%B3%E5%90%88-%E5%A4%A7/dp/4046040076\">独学プログラマーのためのAIアプリ開発がわかる本 </a>。詳細は<a href=\"https://freelance.indieverse.co.jp/media/company\">運営者情報</a>をご覧ください。 <a href=\"https://twitter.com/never_be_a_pm\">@never_be_a_pm</a>\n</div>\n</p></div>\n</p></div>\n<h2>配列の基本的なsort</h2>\n<p>Rubyの配列をソートする場合、<code>sort</code>メソッドを使用します。</p>\n<p>このメソッドは、配列の要素を比較して、昇順に並び替えます。</p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-ruby\" data-lang=\"Ruby\"><code>numbers = [5, 3, 8, 6, 1]\r\nsorted_numbers = numbers.sort\r\nputs sorted_numbers</code></pre>\n</div>\n<p>実行結果:</p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-ruby\" data-lang=\"Ruby\"><code>[1, 3, 5, 6, 8]</code></pre>\n</div>\n<p>これが配列をソートする最も基本的な方法です。文字列も同様の方法でソートできます。</p>\n<h2>文字列のソート方法</h2>\n<p>文字列を含む配列をソートするには、同じく<code>sort</code>メソッドを利用します。</p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-ruby\" data-lang=\"Ruby\"><code>words = [\"banana\", \"apple\", \"cherry\"]\r\nsorted_words = words.sort\r\nputs sorted_words</code></pre>\n</div>\n<p>実行結果:</p>\n<p><code>[\"apple\", \"banana\", \"cherry\"]</code></p>\n<p>文字列は辞書順にソートされます。</p>\n<h2>配列内のオブジェクトをソート</h2>\n<p>配列内にハッシュやオブジェクトが含まれる場合、<code>sort</code>メソッドをカスタマイズすることでソートできます。ブロックを使用して、特定の属性やキーに基づいてソートします。</p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-ruby\" data-lang=\"Ruby\"><code>people = [{name: \"John\", age: 45}, {name: \"Anna\", age: 32}, {name: \"Peter\", age: 28}]\r\nsorted_people = people.sort_by { |person| person[:age] }\r\nputs sorted_people</code></pre>\n</div>\n<p>実行結果:</p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-ruby\" data-lang=\"Ruby\"><code>[{:name=&gt;\"Peter\", :age=&gt;28}, {:name=&gt;\"Anna\", :age=&gt;32}, {:name=&gt;\"John\", :age=&gt;45}]</code></pre>\n</div>\n<p>この例では、人々を年齢で昇順にソートしています。</p>\n<p>関連：</p>\n<h3>複数の条件でのソート方法</h3>\n<p><code>sort_by</code>メソッドは、複数の条件でソートするのにも便利です。例えば、名前でソートし、同じ名前の場合は年齢でソートしたい場合は次のようにします。</p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-ruby\" data-lang=\"Ruby\"><code>people = [{name: \"John\", age: 45}, {name: \"Anna\", age: 32}, {name: \"John\", age: 28}]\r\nsorted_people = people.sort_by { |person| [person[:name], person[:age]] }\r\nputs sorted_people</code></pre>\n</div>\n<p>実行結果:</p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-ruby\" data-lang=\"Ruby\"><code>[{:name=&gt;\"Anna\", :age=&gt;32}, {:name=&gt;\"John\", :age=&gt;28}, {:name=&gt;\"John\", :age=&gt;45}]</code></pre>\n</div>\n<p>名前で最初にソートし、それから年齢でソートしています。</p>\n<h2>SortとSort_byメソッドの違い</h2>\n<p><code>sort</code>と<code>sort_by</code>メソッドの違いを理解することは、Rubyでのソート操作において重要です。基本的に、<code>sort</code>メソッドはシンプルな配列のソートに適していますが、<code>sort_by</code>メソッドはより複雑なデータ構造のソートや、複数の条件に基づくソートに適しています。</p>\n<p><code>sort</code>メソッドは、ブロックを利用して要素間の比較を行い、その結果に基づいて配列をソートします。</p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-ruby\" data-lang=\"Ruby\"><code>numbers = [5, 3, 8, 6, 1]\r\nsorted_numbers = numbers.sort { |a, b| a &lt;=&gt; b }\r\nputs sorted_numbers</code></pre>\n</div>\n<p>実行結果:</p>\n<p><code>[1, 3, 5, 6, 8]</code></p>\n<p>一方、<code>sort_by</code>メソッドは、各要素から特定の属性や値を抽出し、それらを比較してソートする場合に適しています。例えば、オブジェクトの特定の属性に基づいてソートする場合などです。</p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-ruby\" data-lang=\"Ruby\"><code>people = [{name: \"John\", age: 45}, {name: \"Anna\", age: 32}, {name: \"Peter\", age: 28}]\r\nsorted_people = people.sort_by { |person| person[:age] }\r\nputs sorted_people</code></pre>\n</div>\n<p>実行結果:</p>\n<p><code>[{:name=&gt;\"Peter\", :age=&gt;28}, {:name=&gt;\"Anna\", :age=&gt;32}, {:name=&gt;\"John\", :age=&gt;45}]</code></p>\n<h2>Sortメソッドで降順に並び替える</h2>\n<h3>逆順ソートの基本</h3>\n<p><code>sort</code>メソッドを使って配列の要素を降順にソートするには、ブロック内の比較演算子の向きを変えるか、<code>sort</code>メソッド後に<code>reverse</code>メソッドをチェーンさせます。</p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-ruby\" data-lang=\"Ruby\"><code>numbers = [5, 3, 8, 6, 1]\r\ndesc_sorted_numbers = numbers.sort.reverse\r\nputs desc_sorted_numbers</code></pre>\n</div>\n<p>実行結果:</p>\n<p><code>[8, 6, 5, 3, 1]</code></p>\n<p>また</p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-ruby\" data-lang=\"Ruby\"><code>numbers = [5, 3, 8, 6, 1]\r\ndesc_sorted_numbers = numbers.sort { |a, b| b &lt;=&gt; a }\r\nputs desc_sorted_numbers</code></pre>\n</div>\n<p>実行結果:</p>\n<p><code>[8, 6, 5, 3, 1]</code></p>\n<p>このようにして、降順にソートします。</p>\n<h2>Sort_byメソッドでハッシュをソート</h2>\n<p><code>sort_by</code>メソッドは、ハッシュやオブジェクトなどの構造化データをソートする際に非常に役立ちます。例えば、ハッシュのバリューに基づいてソートしたい場合は以下のようにします。</p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-ruby\" data-lang=\"Ruby\"><code># ハッシュをキーでソート\r\nh = { \"banana\" =&gt; 3, \"apple\" =&gt; 1, \"cherry\" =&gt; 2 }\r\nsorted_h = h.sort_by { |key, value| key }\r\nputs sorted_h</code></pre>\n</div>\n<p>実行結果:</p>\n<p><code>[[\"apple\", 1], [\"banana\", 3], [\"cherry\", 2]]</code></p>\n<h2>ハッシュをバリューでソート</h2>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-ruby\" data-lang=\"Ruby\"><code>h = { \"banana\" =&gt; 3, \"apple\" =&gt; 1, \"cherry\" =&gt; 2 }\r\nsorted_h = h.sort_by { |key, value| value }\r\nputs sorted_h</code></pre>\n</div>\n<p>実行結果:</p>\n<p><code>[[\"apple\", 1], [\"cherry\", 2], [\"banana\", 3]]</code></p>\n<p>このように<code>sort_by</code>メソッドを利用することで、キーまたはバリューに基づくソートが容易になります。</p>\n<p>関連：<a href=\"https://freelance.indieverse.co.jp/media/programming/ruby/ruby-hash\">【Ruby】 hashの使い方完全マニュアル | 追加, 更新, 削除, 高度な利用方法について現役エンジニアが解説します</a></p>\n<p><a href=\"https://freelance.indieverse.co.jp/media/programming/ruby/ruby-hash\" target=\"_blank\" class=\"related-article-container\"><br />\n\t\t\t\t\t<span class=\"related-article__label\">合わせて読みたい</span></p>\n<div class=\"related-article__image\" style=\"background-image:url(https://freelance.indieverse.co.jp/media/wp-content/uploads/2022/11/code-g7e399f5d0_1280-150x150.jpg)\"></div>\n<div class=\"related-article__content\">\n<div class=\"related-article__title\">【Ruby】 hashの使い方完全マニュアル | 追加, 更新, 削除, 高度な利用方法について現役エンジニアが解説します</div>\n<div class=\"related-article__description\">RubyのHashの使い方を基礎から応用まで解説。配列との違い、追加・更新・削除、key&#8230;</div>\n</p></div>\n<p>\t\t\t\t</a></p>\n<h2>破壊的メソッドと非破壊的メソッドの使い分け:sortとsort!</h2>\n<p>Rubyでは、メソッド名に感嘆符<code>!</code>がつくメソッドは、その操作がオブジェクトを変更する「破壊的メソッド」であることを示します。<code>sort</code>メソッドにも破壊的なバージョンである<code>sort!</code>が存在し、このメソッドは元の配列自体をソートして変更します。</p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-ruby\" data-lang=\"Ruby\"><code>numbers = [5, 3, 8, 6, 1]\r\nnumbers.sort!\r\nputs numbers</code></pre>\n</div>\n<p>実行結果:</p>\n<p><code>[1, 3, 5, 6, 8]</code></p>\n<p>非破壊的な<code>sort</code>メソッドの場合、ソートされた新しい配列が返されますが、元の配列は変更されません。これに対して、<code>sort!</code>メソッドは元の配列自体を変更します。</p>\n<h2>&lt;=&gt;演算子の役割と比較方法</h2>\n<p><code>sort</code>や<code>sort_by</code>メソッドでは、ブロックを使用することで、より複雑なソート処理を実装することが可能です。ブロック内で比較する要素を具体的に指定し、カスタマイズしたソート処理を実現できます。</p>\n<p><code>&lt;=&gt;</code>演算子（スペースシップ演算子）は、ソート処理において重要な役割を担います。この演算子は、左のオペランドが右のオペランドより小さい時に<code>-1</code>、等しい時に<code>0</code>、大きい時に<code>1</code>を返します。これにより、<code>sort</code>メソッドのブロック内で要素同士を比較し、ソート順を決定します。</p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-ruby\" data-lang=\"Ruby\"><code>words = [\"banana\", \"apple\", \"cherry\"]\r\nsorted_words = words.sort do |a, b|\r\na.length &lt;=&gt; b.length\r\nend\r\nputs sorted_words</code></pre>\n</div>\n<p>実行結果:</p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-ruby\" data-lang=\"Ruby\"><code>[\"apple\", \"banana\", \"cherry\"]</code></pre>\n</div>\n<p>このコードでは、文字列の長さを基準にソートしています。<code>&lt;=&gt;</code>演算子は、比較の結果に基づいて配列をソートする際に便利なツールです。</p>\n<h2>複雑な配列のソート方法</h2>\n<h3>一次元配列と二次元配列のソート方法</h3>\n<p>Rubyでは、単純な一次元配列だけでなく、二次元配列のソートも容易に行えます。二次元配列をソートする場合、<code>sort</code>または<code>sort_by</code>メソッドを使用し、ソートの基準となる要素をブロック内で指定します。</p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-ruby\" data-lang=\"Ruby\"><code>array = [[2, 3], [1, 4], [3, 1]]\r\nsorted_array = array.sort_by { |element| element[0] }\r\nputs sorted_array.inspect</code></pre>\n</div>\n<p>実行結果:</p>\n<p><code>[[1, 4], [2, 3], [3, 1]]</code></p>\n<p>この例では、二次元配列の各要素（配列）の最初の数を基準にしてソートしています。<code>sort_by</code>メソッドを活用することで、複雑なデータ構造を持つ配列でも柔軟にソート処理を行うことが可能です。</p>\n<h2>RubyのsortをRails案件で活かすには</h2>\n<p><code>sort</code>や<code>sort_by</code>は、Ruby on Railsの実務でも一覧表示や集計結果の整理で使われます。DB側で並び替えるべき処理と、Ruby側で一時的に並び替える処理を切り分けられると、読みやすさとパフォーマンスの両方を意識した実装に近づきます。</p>\n<p>Rails開発でどのような一覧画面・検索機能・管理画面の実装が求められているかを知りたい方は、<a href=\"https://freelance.indieverse.co.jp/job_listings/skills/389\">Ruby on Rails案件一覧</a>で実際の案件内容を確認しておくと、学習内容を実務に結びつけやすくなります。</p>\n","protected":false},"excerpt":{"rendered":"<p>現役Rubyエンジニアがsortとsort_byの違いと使い分けを解説。配列・文字列・ハッシュの昇順/降順、複数条件やブロックでのソート手法を効率的に学べます。</p>\n","protected":false},"author":1,"featured_media":54391,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[190],"tags":[286],"class_list":["post-79874","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ruby","tag-ruby"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.3 - aioseo.com -->\n\t<meta name=\"description\" content=\"現役Rubyエンジニアがsortとsort_byの違いと使い分けを解説。配列・文字列・ハッシュの昇順/降順、複数条件やブロックでのソート手法を効率的に学べます。\" />\n\t<meta name=\"robots\" content=\"max-image-preview:large\" />\n\t<meta name=\"author\" content=\"DAI\"/>\n\t<link rel=\"canonical\" href=\"https://freelance.indieverse.co.jp/media/programming/ruby/ruby-sort\" />\n\t\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.3\" />\n\t\t<meta property=\"og:locale\" content=\"ja_JP\" />\n\t\t<meta property=\"og:site_name\" content=\"インディバースフリーランスメディア - Indieverse Freelanceの公式メディアです\" />\n\t\t<meta property=\"og:type\" content=\"article\" />\n\t\t<meta property=\"og:title\" content=\"Rubyのsortメソッドの使い方 | sort_byとの違いも解説 - インディバースフリーランスメディア\" />\n\t\t<meta property=\"og:description\" content=\"現役Rubyエンジニアがsortとsort_byの違いと使い分けを解説。配列・文字列・ハッシュの昇順/降順、複数条件やブロックでのソート手法を効率的に学べます。\" />\n\t\t<meta property=\"og:url\" content=\"https://freelance.indieverse.co.jp/media/programming/ruby/ruby-sort\" />\n\t\t<meta property=\"article:published_time\" content=\"2024-03-23T04:56:24+00:00\" />\n\t\t<meta property=\"article:modified_time\" content=\"2026-10-07T01:58:58+00:00\" />\n\t\t<meta name=\"twitter:card\" content=\"summary\" />\n\t\t<meta name=\"twitter:title\" content=\"Rubyのsortメソッドの使い方 | sort_byとの違いも解説 - インディバースフリーランスメディア\" />\n\t\t<meta name=\"twitter:description\" content=\"現役Rubyエンジニアがsortとsort_byの違いと使い分けを解説。配列・文字列・ハッシュの昇順/降順、複数条件やブロックでのソート手法を効率的に学べます。\" />\n\t\t<script type=\"application/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\/\\/schema.org\",\"@graph\":[{\"@type\":\"BlogPosting\",\"@id\":\"https:\\/\\/freelance.dividable.net\\/programming\\/ruby\\/ruby-sort#blogposting\",\"name\":\"Ruby\\u306esort\\u30e1\\u30bd\\u30c3\\u30c9\\u306e\\u4f7f\\u3044\\u65b9 | sort_by\\u3068\\u306e\\u9055\\u3044\\u3082\\u89e3\\u8aac - \\u30a4\\u30f3\\u30c7\\u30a3\\u30d0\\u30fc\\u30b9\\u30d5\\u30ea\\u30fc\\u30e9\\u30f3\\u30b9\\u30e1\\u30c7\\u30a3\\u30a2\",\"headline\":\"Ruby\\u306esort\\u30e1\\u30bd\\u30c3\\u30c9\\u306e\\u4f7f\\u3044\\u65b9 | sort_by\\u3068\\u306e\\u9055\\u3044\\u3082\\u89e3\\u8aac\",\"author\":{\"@id\":\"https:\\/\\/freelance.dividable.net\\/author\\/wpmaster#author\"},\"publisher\":{\"@id\":\"https:\\/\\/freelance.dividable.net\\/#organization\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\/\\/freelance.dividable.net\\/wp-content\\/uploads\\/2022\\/11\\/code-g7e399f5d0_1280.jpg\",\"width\":1280,\"height\":853},\"datePublished\":\"2024-03-23T13:56:24+09:00\",\"dateModified\":\"2026-10-07T10:58:58+09:00\",\"inLanguage\":\"ja\",\"mainEntityOfPage\":{\"@id\":\"https:\\/\\/freelance.dividable.net\\/programming\\/ruby\\/ruby-sort#webpage\"},\"isPartOf\":{\"@id\":\"https:\\/\\/freelance.dividable.net\\/programming\\/ruby\\/ruby-sort#webpage\"},\"articleSection\":\"Ruby, Ruby\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\/\\/freelance.dividable.net\\/programming\\/ruby\\/ruby-sort#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\/\\/freelance.dividable.net\\/#listItem\",\"position\":1,\"name\":\"\\u30db\\u30fc\\u30e0\",\"item\":\"https:\\/\\/freelance.dividable.net\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\/\\/freelance.dividable.net\\/category\\/programming#listItem\",\"name\":\"\\u30d7\\u30ed\\u30b0\\u30e9\\u30df\\u30f3\\u30b0\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\/\\/freelance.dividable.net\\/category\\/programming#listItem\",\"position\":2,\"name\":\"\\u30d7\\u30ed\\u30b0\\u30e9\\u30df\\u30f3\\u30b0\",\"item\":\"https:\\/\\/freelance.dividable.net\\/category\\/programming\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\/\\/freelance.dividable.net\\/category\\/programming\\/ruby#listItem\",\"name\":\"Ruby\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\/\\/freelance.dividable.net\\/#listItem\",\"name\":\"\\u30db\\u30fc\\u30e0\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\/\\/freelance.dividable.net\\/category\\/programming\\/ruby#listItem\",\"position\":3,\"name\":\"Ruby\",\"item\":\"https:\\/\\/freelance.dividable.net\\/category\\/programming\\/ruby\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\/\\/freelance.dividable.net\\/programming\\/ruby\\/ruby-sort#listItem\",\"name\":\"Ruby\\u306esort\\u30e1\\u30bd\\u30c3\\u30c9\\u306e\\u4f7f\\u3044\\u65b9 | sort_by\\u3068\\u306e\\u9055\\u3044\\u3082\\u89e3\\u8aac\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\/\\/freelance.dividable.net\\/category\\/programming#listItem\",\"name\":\"\\u30d7\\u30ed\\u30b0\\u30e9\\u30df\\u30f3\\u30b0\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\/\\/freelance.dividable.net\\/programming\\/ruby\\/ruby-sort#listItem\",\"position\":4,\"name\":\"Ruby\\u306esort\\u30e1\\u30bd\\u30c3\\u30c9\\u306e\\u4f7f\\u3044\\u65b9 | sort_by\\u3068\\u306e\\u9055\\u3044\\u3082\\u89e3\\u8aac\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\/\\/freelance.dividable.net\\/category\\/programming\\/ruby#listItem\",\"name\":\"Ruby\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\/\\/freelance.dividable.net\\/#organization\",\"name\":\"Indieverse Freelance\",\"description\":\"Indieverse Freelance\\u306e\\u516c\\u5f0f\\u30e1\\u30c7\\u30a3\\u30a2\\u3067\\u3059\",\"url\":\"https:\\/\\/freelance.dividable.net\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\/\\/freelance.dividable.net\\/author\\/wpmaster#author\",\"url\":\"https:\\/\\/freelance.dividable.net\\/author\\/wpmaster\",\"name\":\"DAI\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\/\\/freelance.dividable.net\\/programming\\/ruby\\/ruby-sort#webpage\",\"url\":\"https:\\/\\/freelance.dividable.net\\/programming\\/ruby\\/ruby-sort\",\"name\":\"Ruby\\u306esort\\u30e1\\u30bd\\u30c3\\u30c9\\u306e\\u4f7f\\u3044\\u65b9 | sort_by\\u3068\\u306e\\u9055\\u3044\\u3082\\u89e3\\u8aac - \\u30a4\\u30f3\\u30c7\\u30a3\\u30d0\\u30fc\\u30b9\\u30d5\\u30ea\\u30fc\\u30e9\\u30f3\\u30b9\\u30e1\\u30c7\\u30a3\\u30a2\",\"description\":\"\\u73fe\\u5f79Ruby\\u30a8\\u30f3\\u30b8\\u30cb\\u30a2\\u304csort\\u3068sort_by\\u306e\\u9055\\u3044\\u3068\\u4f7f\\u3044\\u5206\\u3051\\u3092\\u89e3\\u8aac\\u3002\\u914d\\u5217\\u30fb\\u6587\\u5b57\\u5217\\u30fb\\u30cf\\u30c3\\u30b7\\u30e5\\u306e\\u6607\\u9806\\/\\u964d\\u9806\\u3001\\u8907\\u6570\\u6761\\u4ef6\\u3084\\u30d6\\u30ed\\u30c3\\u30af\\u3067\\u306e\\u30bd\\u30fc\\u30c8\\u624b\\u6cd5\\u3092\\u52b9\\u7387\\u7684\\u306b\\u5b66\\u3079\\u307e\\u3059\\u3002\",\"inLanguage\":\"ja\",\"isPartOf\":{\"@id\":\"https:\\/\\/freelance.dividable.net\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\/\\/freelance.dividable.net\\/programming\\/ruby\\/ruby-sort#breadcrumblist\"},\"author\":{\"@id\":\"https:\\/\\/freelance.dividable.net\\/author\\/wpmaster#author\"},\"creator\":{\"@id\":\"https:\\/\\/freelance.dividable.net\\/author\\/wpmaster#author\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\/\\/freelance.dividable.net\\/wp-content\\/uploads\\/2022\\/11\\/code-g7e399f5d0_1280.jpg\",\"@id\":\"https:\\/\\/freelance.dividable.net\\/programming\\/ruby\\/ruby-sort\\/#mainImage\",\"width\":1280,\"height\":853},\"primaryImageOfPage\":{\"@id\":\"https:\\/\\/freelance.dividable.net\\/programming\\/ruby\\/ruby-sort#mainImage\"},\"datePublished\":\"2024-03-23T13:56:24+09:00\",\"dateModified\":\"2026-10-07T10:58:58+09:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\/\\/freelance.dividable.net\\/#website\",\"url\":\"https:\\/\\/freelance.dividable.net\\/\",\"name\":\"\\u30a4\\u30f3\\u30c7\\u30a3\\u30d0\\u30fc\\u30b9\\u30d5\\u30ea\\u30fc\\u30e9\\u30f3\\u30b9\\u30e1\\u30c7\\u30a3\\u30a2\",\"description\":\"Indieverse Freelance\\u306e\\u516c\\u5f0f\\u30e1\\u30c7\\u30a3\\u30a2\\u3067\\u3059\",\"inLanguage\":\"ja\",\"publisher\":{\"@id\":\"https:\\/\\/freelance.dividable.net\\/#organization\"}}]}\n\t\t</script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Rubyのsortメソッドの使い方 | sort_byとの違いも解説 - インディバースフリーランスメディア","description":"現役Rubyエンジニアがsortとsort_byの違いと使い分けを解説。配列・文字列・ハッシュの昇順/降順、複数条件やブロックでのソート手法を効率的に学べます。","canonical_url":"https://freelance.indieverse.co.jp/media/programming/ruby/ruby-sort","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https://schema.org","@graph":[{"@type":"BlogPosting","@id":"https://freelance.indieverse.co.jp/media/programming/ruby/ruby-sort#blogposting","name":"Rubyのsortメソッドの使い方 | sort_byとの違いも解説 - インディバースフリーランスメディア","headline":"Rubyのsortメソッドの使い方 | sort_byとの違いも解説","author":{"@id":"https://freelance.indieverse.co.jp/media/author/wpmaster#author"},"publisher":{"@id":"https://freelance.indieverse.co.jp/media/#organization"},"image":{"@type":"ImageObject","url":"https://freelance.indieverse.co.jp/media/wp-content/uploads/2022/11/code-g7e399f5d0_1280.jpg","width":1280,"height":853},"datePublished":"2024-03-23T13:56:24+09:00","dateModified":"2026-10-07T10:58:58+09:00","inLanguage":"ja","mainEntityOfPage":{"@id":"https://freelance.indieverse.co.jp/media/programming/ruby/ruby-sort#webpage"},"isPartOf":{"@id":"https://freelance.indieverse.co.jp/media/programming/ruby/ruby-sort#webpage"},"articleSection":"Ruby, Ruby"},{"@type":"BreadcrumbList","@id":"https://freelance.indieverse.co.jp/media/programming/ruby/ruby-sort#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https://freelance.indieverse.co.jp/media/#listItem","position":1,"name":"ホーム","item":"https://freelance.indieverse.co.jp/media/","nextItem":{"@type":"ListItem","@id":"https://freelance.indieverse.co.jp/media/category/programming#listItem","name":"プログラミング"}},{"@type":"ListItem","@id":"https://freelance.indieverse.co.jp/media/category/programming#listItem","position":2,"name":"プログラミング","item":"https://freelance.indieverse.co.jp/media/category/programming","nextItem":{"@type":"ListItem","@id":"https://freelance.indieverse.co.jp/media/category/programming/ruby#listItem","name":"Ruby"},"previousItem":{"@type":"ListItem","@id":"https://freelance.indieverse.co.jp/media/#listItem","name":"ホーム"}},{"@type":"ListItem","@id":"https://freelance.indieverse.co.jp/media/category/programming/ruby#listItem","position":3,"name":"Ruby","item":"https://freelance.indieverse.co.jp/media/category/programming/ruby","nextItem":{"@type":"ListItem","@id":"https://freelance.indieverse.co.jp/media/programming/ruby/ruby-sort#listItem","name":"Rubyのsortメソッドの使い方 | sort_byとの違いも解説"},"previousItem":{"@type":"ListItem","@id":"https://freelance.indieverse.co.jp/media/category/programming#listItem","name":"プログラミング"}},{"@type":"ListItem","@id":"https://freelance.indieverse.co.jp/media/programming/ruby/ruby-sort#listItem","position":4,"name":"Rubyのsortメソッドの使い方 | sort_byとの違いも解説","previousItem":{"@type":"ListItem","@id":"https://freelance.indieverse.co.jp/media/category/programming/ruby#listItem","name":"Ruby"}}]},{"@type":"Organization","@id":"https://freelance.indieverse.co.jp/media/#organization","name":"Indieverse Freelance","description":"Indieverse Freelanceの公式メディアです","url":"https://freelance.indieverse.co.jp/media/"},{"@type":"Person","@id":"https://freelance.indieverse.co.jp/media/author/wpmaster#author","url":"https://freelance.indieverse.co.jp/media/author/wpmaster","name":"DAI"},{"@type":"WebPage","@id":"https://freelance.indieverse.co.jp/media/programming/ruby/ruby-sort#webpage","url":"https://freelance.indieverse.co.jp/media/programming/ruby/ruby-sort","name":"Rubyのsortメソッドの使い方 | sort_byとの違いも解説 - インディバースフリーランスメディア","description":"現役Rubyエンジニアがsortとsort_byの違いと使い分けを解説。配列・文字列・ハッシュの昇順/降順、複数条件やブロックでのソート手法を効率的に学べます。","inLanguage":"ja","isPartOf":{"@id":"https://freelance.indieverse.co.jp/media/#website"},"breadcrumb":{"@id":"https://freelance.indieverse.co.jp/media/programming/ruby/ruby-sort#breadcrumblist"},"author":{"@id":"https://freelance.indieverse.co.jp/media/author/wpmaster#author"},"creator":{"@id":"https://freelance.indieverse.co.jp/media/author/wpmaster#author"},"image":{"@type":"ImageObject","url":"https://freelance.indieverse.co.jp/media/wp-content/uploads/2022/11/code-g7e399f5d0_1280.jpg","@id":"https://freelance.indieverse.co.jp/media/programming/ruby/ruby-sort/#mainImage","width":1280,"height":853},"primaryImageOfPage":{"@id":"https://freelance.indieverse.co.jp/media/programming/ruby/ruby-sort#mainImage"},"datePublished":"2024-03-23T13:56:24+09:00","dateModified":"2026-10-07T10:58:58+09:00"},{"@type":"WebSite","@id":"https://freelance.indieverse.co.jp/media/#website","url":"https://freelance.indieverse.co.jp/media/","name":"インディバースフリーランスメディア","description":"Indieverse Freelanceの公式メディアです","inLanguage":"ja","publisher":{"@id":"https://freelance.indieverse.co.jp/media/#organization"}}]},"og:locale":"ja_JP","og:site_name":"インディバースフリーランスメディア - Indieverse Freelanceの公式メディアです","og:type":"article","og:title":"Rubyのsortメソッドの使い方 | sort_byとの違いも解説 - インディバースフリーランスメディア","og:description":"現役Rubyエンジニアがsortとsort_byの違いと使い分けを解説。配列・文字列・ハッシュの昇順/降順、複数条件やブロックでのソート手法を効率的に学べます。","og:url":"https://freelance.indieverse.co.jp/media/programming/ruby/ruby-sort","article:published_time":"2024-03-23T04:56:24+00:00","article:modified_time":"2026-10-07T01:58:58+00:00","twitter:card":"summary","twitter:title":"Rubyのsortメソッドの使い方 | sort_byとの違いも解説 - インディバースフリーランスメディア","twitter:description":"現役Rubyエンジニアがsortとsort_byの違いと使い分けを解説。配列・文字列・ハッシュの昇順/降順、複数条件やブロックでのソート手法を効率的に学べます。"},"aioseo_meta_data":{"post_id":"79874","title":null,"description":null,"keywords":null,"keyphrases":{"focus":{"keyphrase":"","score":0,"analysis":{"keyphraseInTitle":{"score":0,"maxScore":9,"error":1}}},"additional":[]},"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":"","og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":{"id":"#aioseo-article-67f5da25f419c","slug":"article","graphName":"Article","label":"記事","properties":{"type":"BlogPosting","name":"#post_title","headline":"#post_title","description":"#post_excerpt","image":"","keywords":"","author":{"name":"#author_name","url":"#author_url"},"dates":{"include":true,"datePublished":"","dateModified":""}}},"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":"{\"article\":{\"articleType\":\"BlogPosting\"},\"course\":{\"name\":\"\",\"description\":\"\",\"provider\":\"\"},\"faq\":{\"pages\":[]},\"product\":{\"reviews\":[]},\"recipe\":{\"ingredients\":[],\"instructions\":[],\"keywords\":[]},\"software\":{\"reviews\":[],\"operatingSystems\":[]},\"webPage\":{\"webPageType\":\"WebPage\"}}","pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":"-1","robots_max_videopreview":"-1","robots_max_imagepreview":"large","priority":null,"frequency":"default","local_seo":null,"limit_modified_date":false,"created":"2024-03-23 04:17:52","updated":"2025-09-08 22:44:50","focus_keyword":null,"additional_keywords":null,"truseo_locale":null,"ai":null,"breadcrumb_settings":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https://freelance.indieverse.co.jp/media/\" title=\"ホーム\">ホーム</a>\n\t\t</span><span class=\"aioseo-breadcrumb-separator\">»</span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https://freelance.indieverse.co.jp/media/category/programming\" title=\"プログラミング\">プログラミング</a>\n\t\t</span><span class=\"aioseo-breadcrumb-separator\">»</span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https://freelance.indieverse.co.jp/media/category/programming/ruby\" title=\"Ruby\">Ruby</a>\n\t\t</span><span class=\"aioseo-breadcrumb-separator\">»</span><span class=\"aioseo-breadcrumb\">\n\t\t\tRubyのsortメソッドの使い方 | sort_byとの違いも解説\n\t\t</span></div>","aioseo_breadcrumb_json":[{"label":"ホーム","link":"https://freelance.indieverse.co.jp/media/"},{"label":"プログラミング","link":"https://freelance.indieverse.co.jp/media/category/programming"},{"label":"Ruby","link":"https://freelance.indieverse.co.jp/media/category/programming/ruby"},{"label":"Rubyのsortメソッドの使い方 | sort_byとの違いも解説","link":"https://freelance.indieverse.co.jp/media/programming/ruby/ruby-sort"}],"meta_description":"現役Rubyエンジニアがsortとsort_byの違いと使い分けを解説。配列・文字列・ハッシュの昇順/降順、複数条件やブロックでのソート手法を効率的に学べます。","_links":{"self":[{"href":"https://freelance.indieverse.co.jp/media/wp-json/wp/v2/posts/79874","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https://freelance.indieverse.co.jp/media/wp-json/wp/v2/posts"}],"about":[{"href":"https://freelance.indieverse.co.jp/media/wp-json/wp/v2/types/post"}],"author":[{"embeddable":true,"href":"https://freelance.indieverse.co.jp/media/wp-json/wp/v2/users/1"}],"replies":[{"embeddable":true,"href":"https://freelance.indieverse.co.jp/media/wp-json/wp/v2/comments?post=79874"}],"version-history":[{"count":10,"href":"https://freelance.indieverse.co.jp/media/wp-json/wp/v2/posts/79874/revisions"}],"predecessor-version":[{"id":98614,"href":"https://freelance.indieverse.co.jp/media/wp-json/wp/v2/posts/79874/revisions/98614"}],"wp:featuredmedia":[{"embeddable":true,"href":"https://freelance.indieverse.co.jp/media/wp-json/wp/v2/media/54391"}],"wp:attachment":[{"href":"https://freelance.indieverse.co.jp/media/wp-json/wp/v2/media?parent=79874"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https://freelance.indieverse.co.jp/media/wp-json/wp/v2/categories?post=79874"},{"taxonomy":"post_tag","embeddable":true,"href":"https://freelance.indieverse.co.jp/media/wp-json/wp/v2/tags?post=79874"}],"curies":[{"name":"wp","href":"https://api.w.org/{rel}","templated":true}]}}