From: barelyknown@... Date: 2015-12-28T14:12:35+00:00 Subject: [ruby-core:72553] [Ruby trunk - Bug #11907] sort_by does not preserve order with arrays with 7+ items and a block that returns the same value Issue #11907 has been updated by Sean Devine. Description updated ---------------------------------------- Bug #11907: sort_by does not preserve order with arrays with 7+ items and a block that returns the same value https://bugs.ruby-lang.org/issues/11907#change-55814 * Author: Sean Devine * Status: Open * Priority: Normal * Assignee: * ruby -v: 2.3.0 * Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN, 2.3: UNKNOWN ---------------------------------------- **TL;DR Arrays with more than 6 items do not have their order preserved if sorted with sort_by and the sort_by block returns the same value for each.** Let���s start with a simple array of 6 items and sort it by the same value using the sort_by method. In Ruby 2.3 and Ruby 2.2.3, the order of the list will remain the same. ``` list = [1,2,3,4,5,6] list.sort_by { |item| nil } => [1,2,3,4,5,6] ``` But, let���s add one item. ``` list = [1,2,3,4,5,6,7] # with Ruby 2.2.3 list.sort_by { |item| nil } => [1, 2, 3, 4, 5, 6, 7] # with Ruby 2.3.0 list.sort_by { |item| nil } => [4, 2, 3, 1, 5, 6, 7] ``` Whoa! In Ruby 2.3, the order of the first and fourth items will be swapped. And, if we have a list of 8 or more items, the order of the first and last items will be swapped. ``` list = [1,2,3,4,5,6,7,8] list.sort_by { |item| nil } => [8, 2, 3, 4, 5, 6, 7, 1] ``` -- https://bugs.ruby-lang.org/ Unsubscribe: