"CONF NO -6ren">
gpt4 book ai didi

arrays - sort_by 哈希数组未在 ruby​​ 中给出预期结果

转载 作者:数据小太阳 更新时间:2023-10-29 07:55:11 26 4
gpt4 key购买 nike

我有下面的数组

arr = [
{ nbr: "979276030", des: "CONF NO COST 30 Refi Plus <= 105 SM SRVR", status: "2C", milestone: "Processing", dt_time: "03/23/2016 12:00 AM", name: "Brad Pacocha" },
{ nbr: "910482832", des: "CONF NO COST 30 Refi Plus <= 105 SM SRVR", status: "2C", milestone: "Processing", dt_time: "08/23/2015 12:00 AM", name: "Aracely Bogan" },
{ nbr: "819205275", des: "CONF NO COST 30 Refi Plus <= 105 SM SRVR", status: "2C", milestone: "Processing", dt_time: "09/02/2015 12:00 AM", name: "Kelli Moore" },
{ nbr: "755667162", des: "CONF NO COST 30 Refi Plus <= 105 SM SRVR", status: "2C", milestone: "Processing", dt_time: "10/16/2015 12:00 AM", name: "Trace Auer" },
{ nbr: "561189198", des: "CONF NO COST 30 Refi Plus <= 105 SM SRVR", status: "2C", milestone: "Processing", dt_time: "01/11/2016 12:00 AM", name: "Geoffrey Will" },
{ nbr: "429905984", des: "CONF NO COST 30 Refi Plus <= 105 SM SRVR", status: "2C", milestone: "Processing", dt_time: "04/25/2016 12:00 AM", name: "Martine Berge" },
{ nbr: "421919042", des: "CONF NO COST 30 Refi Plus <= 105 SM SRVR", status: "2C", milestone: "Processing", dt_time: "02/26/2016 12:00 AM", name: "Jewel Bailey" },
{ nbr: "274874145", des: "CONF NO COST 30 Refi Plus <= 105 SM SRVR", status: "2C", milestone: "Processing", dt_time: "04/19/2016 12:00 AM", name: "Wendell Tremblay" },
{ nbr: "254548319", des: "CONF NO COST 30 Refi Plus <= 105 SM SRVR", status: "2C", milestone: "Processing", dt_time: "01/16/2016 12:00 AM", name: "Dewitt Ritchie" },
{ nbr: "250154069", des: "CONF NO COST 30 Refi Plus <= 105 SM SRVR", status: "2C", milestone: "Processing", dt_time: "10/12/2015 12:00 AM", name: "Lisette Wehner" }
]

sort_arr = arr.sort_by { |h| h[:des] }

puts arr
puts '*'*100
puts sort_arr

我期望排序后的结果应该与我的源数组相同,因为键对所有哈希具有相同的值

但我得到以下结果

{:nbr=>"250154069", :des=>"CONF NO COST 30 Refi Plus <= 105 SM SRVR", :status=>"2C", :milestone=>"Processing", :dt_time=>"10/12/2015 12:00 AM", :name=>"Lisette Wehner"}
{:nbr=>"910482832", :des=>"CONF NO COST 30 Refi Plus <= 105 SM SRVR", :status=>"2C", :milestone=>"Processing", :dt_time=>"08/23/2015 12:00 AM", :name=>"Aracely Bogan"}
{:nbr=>"819205275", :des=>"CONF NO COST 30 Refi Plus <= 105 SM SRVR", :status=>"2C", :milestone=>"Processing", :dt_time=>"09/02/2015 12:00 AM", :name=>"Kelli Moore"}
{:nbr=>"755667162", :des=>"CONF NO COST 30 Refi Plus <= 105 SM SRVR", :status=>"2C", :milestone=>"Processing", :dt_time=>"10/16/2015 12:00 AM", :name=>"Trace Auer"}
{:nbr=>"561189198", :des=>"CONF NO COST 30 Refi Plus <= 105 SM SRVR", :status=>"2C", :milestone=>"Processing", :dt_time=>"01/11/2016 12:00 AM", :name=>"Geoffrey Will"}
{:nbr=>"429905984", :des=>"CONF NO COST 30 Refi Plus <= 105 SM SRVR", :status=>"2C", :milestone=>"Processing", :dt_time=>"04/25/2016 12:00 AM", :name=>"Martine Berge"}
{:nbr=>"421919042", :des=>"CONF NO COST 30 Refi Plus <= 105 SM SRVR", :status=>"2C", :milestone=>"Processing", :dt_time=>"02/26/2016 12:00 AM", :name=>"Jewel Bailey"}
{:nbr=>"274874145", :des=>"CONF NO COST 30 Refi Plus <= 105 SM SRVR", :status=>"2C", :milestone=>"Processing", :dt_time=>"04/19/2016 12:00 AM", :name=>"Wendell Tremblay"}
{:nbr=>"254548319", :des=>"CONF NO COST 30 Refi Plus <= 105 SM SRVR", :status=>"2C", :milestone=>"Processing", :dt_time=>"01/16/2016 12:00 AM", :name=>"Dewitt Ritchie"}
{:nbr=>"979276030", :des=>"CONF NO COST 30 Refi Plus <= 105 SM SRVR", :status=>"2C", :milestone=>"Processing", :dt_time=>"03/23/2016 12:00 AM", :name=>"Brad Pacocha"}

交换第一个和最后一个哈希值。

这是一个有效的结果吗?这种行为的原因是什么?

最佳答案

Ruby 的 Array#sort_by 使用快速排序的高效实现。

Quicksort is a comparison sort, meaning that it can sort items of any type for which a "less-than" relation (formally, a total order) is defined. In efficient implementations it is not a stable sort, meaning that the relative order of equal sort items is not preserved. - Wikipedia Quicksort Article

关于arrays - sort_by 哈希数组未在 ruby​​ 中给出预期结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38781938/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com