From: mortee Date: 2007-10-13T07:27:31+09:00 Subject: Re: sort double-dimensional array by column Mr_Tibs wrote: > Thanks mortee. That sort of worked. Unfortunately, it puts those > entries that have the 6-th column = nil, at the start of the sorted > array (instead of at the end). I don't really understand the technique > used. You are sorting by row, but you are somehow defining and using > an array with 2 elements (that's what the square brackets do, right?). Yes. Your code seemed like you want to fall back to the 7th column for ordering when the 6th column is nil in both rows to compare. If you do a sort_by, then it'll sort the items (in this case rows) based on the value the block returns for the item passed to it. And comparing arrays works this way: if the first elements differ, then their order determines the final ordering; if they are the same, then the rest of the arrays get compared, all the way through the last element. So if you provide the mentioned two-element array as a sort key for your rows, then the result will be almost what your original code did. Actually, in the meantime I noticed that when the 6th column is non-nil and equal in both rows to compare, your code returns equal ordering independently of the 7th column's values - while my code falls back to them in this case too. Reagrding your problem with rows with nil in the 6th column going to the start: naturally, sort will yield an ascending order, and the default value substituted in my code is the lowest possible, so they'll get sorted low (that is, to the start). You're of course free to specify any default value you like, so that they get sorted to where you want them. mortee