0
私は歌の以下の配列をソートしようとしています:ルビー:ゼロへの文字列の比較失敗しました(引数エラー)
array = ["Jurassic 5 - What's Golden - hip-hop",
"Action Bronson - Larry Csonka - indie",
"Real Estate - Green Aisles - country",
"Real Estate - It's Real - hip-hop",
"Thundercat - For Love I Come - dance"]
私が欲しいのは、曲の名前に基づいて配列をソートすることです。
array = ["Jurassic 5 - **What's Golden** - hip-hop"]
私は次のコードでこれを実行しようとしました:
array.sort do |a, b|
a = a.split(" - ")
b = b.split(" - ")
a[1] <=> b[1]
a = a.join(" - ")
b = b.join(" - ")
end
私が欲しい結果の配列は次のとおりです。
曲の名前は、例えば、各要素の中間のテキストですArgumentError: comparison of String with 0 failed
from (irb):52:in `>'
from (irb):52:in `sort'
from (irb):52
from C:/Ruby23/bin/irb.cmd:19:in `<main>'
:
array = ["Thundercat - For Love I Come - dance",
"Real Estate - Green Aisles - country",
"Real Estate - It's Real - hip-hop",
"Action Bronson - Larry Csonka - indie",
"Jurassic 5 - What's Golden - hip-hop"]
しかし、私は次のエラーを取得しています
私はPRYで比較される値をチェックしており、両方とも文字列です。
pry(#<MusicLibraryController>)> a.class
=> Array
pry(#<MusicLibraryController>)> a[1].class
=> String
pry(#<MusicLibraryController>)> b.class
=>Array
pry(#<MusicLibraryController>)> b[1].class
=> String
質問:
- は、なぜ私はこのエラーを取得していますか?
- エラーを取り除くにはどうすればよいですか?
- 要素の特定の部分に基づいて配列をソートする方が良いでしょうか?この場合のように、文字列要素のサブ文字列?
あなたの神..ありがとう:) –
私の喜びサー; – Ursus