Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
96 views
in Technique[技术] by (71.8m points)

Ruby sort_by array of hashes

I have an array of hashes that I'd like to be able to sort alphabetically on one of the values.

Array of hashes:

[{"hidden"=>false, "id"=>"5f898f7784743992f24fcb51", "field_int"=>11, "field_string"=>"test"},
 {"hidden"=>false, "id"=>"5f898f7784743992f24fcb51", "field_int"=>11, "field_string"=>"batch update testing"},
 {"hidden"=>false, "id"=>"5f898f7784743992f24fcb51", "field_int"=>11, "field_string"=>"test"},
 {"hidden"=>false, "id"=>"5f898f7784743992f24fcb51", "field_int"=>11, "field_string"=>"batch update testing"},
 {"hidden"=>false, "id"=>"5f898f7784743992f24fcb51", "field_int"=>11, "field_string"=>"test"},
 {"hidden"=>false, "id"=>"5f898f7784743992f24fcb51", "field_int"=>11, "field_string"=>"batch update testing"},
 {"hidden"=>false, "id"=>"5f898f7784743992f24fcb51", "field_int"=>11, "field_string"=>"test"},
 {"hidden"=>false, "id"=>"5f898f7784743992f24fcb51", "field_int"=>11, "field_string"=>"batch update testing"},
 {"hidden"=>false, "id"=>"5f898f7784743992f24fcb51", "field_int"=>11, "field_string"=>"test"},
 {"hidden"=>false, "id"=>"5f898f7784743992f24fcb51", "field_int"=>11, "field_string"=>"batch update testing"},
 {"hidden"=>false, "id"=>"5f898f7784743992f24fcb51", "field_int"=>11, "field_string"=>"test"},
 {"hidden"=>false, "id"=>"5f898f7784743992f24fcb51", "field_int"=>11, "field_string"=>"batch update testing"},
 {"hidden"=>false, "id"=>"5f898f7784743992f24fcb51", "field_int"=>11, "field_string"=>"test"},
 {"hidden"=>false, "id"=>"5f898f7784743992f24fcb51", "field_int"=>11, "field_string"=>"batch update testing"},
 {"hidden"=>false, "id"=>"5f898f7784743992f24fcb51", "field_int"=>22, "field_string"=>"c next"},
 {"hidden"=>false, "id"=>"5f898f7784743992f24fcb51", "field_int"=>33, "field_string"=>"d next"}]

After running sort_by:

records.sort_by { |rec| rec[:field_string]}
=> [{"hidden"=>false, "id"=>"5f898f7784743992f24fcb51", "field_int"=>11, "field_string"=>"test"},
 {"hidden"=>false, "id"=>"5f898f7784743992f24fcb51", "field_int"=>11, "field_string"=>"batch update testing"},
 {"hidden"=>false, "id"=>"5f898f7784743992f24fcb51", "field_int"=>11, "field_string"=>"test"},
 {"hidden"=>false, "id"=>"5f898f7784743992f24fcb51", "field_int"=>11, "field_string"=>"batch update testing"},
 {"hidden"=>false, "id"=>"5f898f7784743992f24fcb51", "field_int"=>11, "field_string"=>"test"},
 {"hidden"=>false, "id"=>"5f898f7784743992f24fcb51", "field_int"=>11, "field_string"=>"batch update testing"},
 {"hidden"=>false, "id"=>"5f898f7784743992f24fcb51", "field_int"=>11, "field_string"=>"test"},
 {"hidden"=>false, "id"=>"5f898f7784743992f24fcb51", "field_int"=>11, "field_string"=>"batch update testing"},
 {"hidden"=>false, "id"=>"5f898f7784743992f24fcb51", "field_int"=>11, "field_string"=>"test"},
 {"hidden"=>false, "id"=>"5f898f7784743992f24fcb51", "field_int"=>11, "field_string"=>"batch update testing"},
 {"hidden"=>false, "id"=>"5f898f7784743992f24fcb51", "field_int"=>11, "field_string"=>"test"},
 {"hidden"=>false, "id"=>"5f898f7784743992f24fcb51", "field_int"=>11, "field_string"=>"batch update testing"},
 {"hidden"=>false, "id"=>"5f898f7784743992f24fcb51", "field_int"=>11, "field_string"=>"test"},
 {"hidden"=>false, "id"=>"5f898f7784743992f24fcb51", "field_int"=>11, "field_string"=>"batch update testing"},
 {"hidden"=>false, "id"=>"5f898f7784743992f24fcb51", "field_int"=>22, "field_string"=>"c next"},
 {"hidden"=>false, "id"=>"5f898f7784743992f24fcb51", "field_int"=>33, "field_string"=>"d next"}]

It sorts correctly for the field_int column, but I cannot understand why it's not working correctly with the field_string. If anyone has any ideas I'd really appreciate hearing them.

Thanks!

question from:https://stackoverflow.com/questions/65829412/ruby-sort-by-array-of-hashes

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The keys in your hashes are strings and not symbols.

records[0][:field_string] is nil

records[0]["field_string"] is "test"

So your sort is comparing nils to nils, and keeps the original order.

You need:

records.sort_by { |rec| rec["field_string"]}

It outputs:

=> [{"hidden"=>false, "id"=>"5f898f7784743992f24fcb51", "field_int"=>11, "field_string"=>"batch update testing"},
 {"hidden"=>false, "id"=>"5f898f7784743992f24fcb51", "field_int"=>11, "field_string"=>"batch update testing"},
 {"hidden"=>false, "id"=>"5f898f7784743992f24fcb51", "field_int"=>11, "field_string"=>"batch update testing"},
 {"hidden"=>false, "id"=>"5f898f7784743992f24fcb51", "field_int"=>11, "field_string"=>"batch update testing"},
 {"hidden"=>false, "id"=>"5f898f7784743992f24fcb51", "field_int"=>11, "field_string"=>"batch update testing"},
 {"hidden"=>false, "id"=>"5f898f7784743992f24fcb51", "field_int"=>11, "field_string"=>"batch update testing"},
 {"hidden"=>false, "id"=>"5f898f7784743992f24fcb51", "field_int"=>11, "field_string"=>"batch update testing"},
 {"hidden"=>false, "id"=>"5f898f7784743992f24fcb51", "field_int"=>22, "field_string"=>"c next"},
 {"hidden"=>false, "id"=>"5f898f7784743992f24fcb51", "field_int"=>33, "field_string"=>"d next"},
 {"hidden"=>false, "id"=>"5f898f7784743992f24fcb51", "field_int"=>11, "field_string"=>"test"},
 {"hidden"=>false, "id"=>"5f898f7784743992f24fcb51", "field_int"=>11, "field_string"=>"test"},
 {"hidden"=>false, "id"=>"5f898f7784743992f24fcb51", "field_int"=>11, "field_string"=>"test"},
 {"hidden"=>false, "id"=>"5f898f7784743992f24fcb51", "field_int"=>11, "field_string"=>"test"},
 {"hidden"=>false, "id"=>"5f898f7784743992f24fcb51", "field_int"=>11, "field_string"=>"test"},
 {"hidden"=>false, "id"=>"5f898f7784743992f24fcb51", "field_int"=>11, "field_string"=>"test"},
 {"hidden"=>false, "id"=>"5f898f7784743992f24fcb51", "field_int"=>11, "field_string"=>"test"}]

It can be confusing, because HashWithIndifferentAccess also exists.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...