M
Michael Gaunnac
------=_NextPart_000_00D6_01C4AD13.37086F20
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Perhaps this has already been covered, but I discovered this idiom for =
doing a descending sort using sort_by.
arr =3D Array.new
(0..20).each {|i| arr.push sprintf('%02d', i)}
p arr
#=3D>["00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", =
"11", "12", "13", "14", "15", "16", "17", "18", "19", "20"]
arr =3D arr.sort_by {|i| [i.slice(0,2).tr('0-9','9876543210')]} # =
descending sort using 9's complement
p arr
#=3D>["20", "19", "18", "17", "16", "15", "14", "13", "12", "11", "10", =
"09", "08", "07", "06", "05", "04", "03", "02", "01", "00"]
Mike
------=_NextPart_000_00D6_01C4AD13.37086F20--
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Perhaps this has already been covered, but I discovered this idiom for =
doing a descending sort using sort_by.
arr =3D Array.new
(0..20).each {|i| arr.push sprintf('%02d', i)}
p arr
#=3D>["00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", =
"11", "12", "13", "14", "15", "16", "17", "18", "19", "20"]
arr =3D arr.sort_by {|i| [i.slice(0,2).tr('0-9','9876543210')]} # =
descending sort using 9's complement
p arr
#=3D>["20", "19", "18", "17", "16", "15", "14", "13", "12", "11", "10", =
"09", "08", "07", "06", "05", "04", "03", "02", "01", "00"]
Mike
------=_NextPart_000_00D6_01C4AD13.37086F20--