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