From: Brett Williams Date: 2002-09-11T04:55:31+09:00 Subject: Re: Multi-level sort idiom? Mike Stok wrote: > In article <1031682194.811634@cswreg.cos.agilent.com>, Brett Williams > wrote: >> Let's say I have an array, each element of which is a 2-dimensional array >> of ints. >> >> I want to sort with the first element in the 2D array taking precedence, >> but using the second as a tiebreaker. [snip] > You can use Numeric's nonzero? e.g. > > x = [[5, 1], [3, 2], [3, 3], [1, 5]] > x.sort { |a, b| (a[0] <=> b[0]).nonzero? || a[1] <=> b[1] } > > => [[1, 5], [3, 2], [3, 3], [5, 1]] Thanks, that's what I was looking for. Now I understand the behavior of Ruby's ||, &&, or, and and better too! -- The key of strategy... is not to choose a path that leads to victory, but to choose so that all paths lead to a victory. -- L.M. Bujold