From: Ammar Ali Date: 2010-11-07T19:48:18+09:00 Subject: Re: sort_by is not stable ? On Sun, Nov 7, 2010 at 12:22 PM, Michel Demazure wrote: > sort_by  is not a stable sorting method (ruby 1.9.2 p0) > > I am doing word statistics in a text file. I have a long array of > entries [word, number], sorted by alphabetic order on word. When I sort > on number, entries with the same number are shuffled. Without code, I have to guess. If I understood your issue correctly, then you need to specify both fields in sort_by block: >> a = [["g", 3], ["c", 3], ["a", 3], ["f", 3], ["e", 2], ["d", 3], ["b", 1]] => [["g", 3], ["c", 3], ["a", 3], ["f", 3], ["e", 2], ["d", 3], ["b", 1]] >> a.sort_by {|e| [e.last, e.first]} => [["b", 1], ["e", 2], ["a", 3], ["c", 3], ["d", 3], ["f", 3], ["g", 3]] Regards, Ammar