From: Robert Klemme Date: 2004-05-15T06:08:52+09:00 Subject: Re: A simpler way to do this? "Phil Roberts" schrieb im Newsbeitrag news:Xns94E9A69321C70philroberts@216.196.97.132... > I have an FXList window showing a list of users from a > database, and as is common for such lists I've > implemented a system for sorting by column, with > reversible sort directions and all. > > The only issue is that It's taken about 6 methods to > get that behaviour. One to issue the sort command , one > to retrieve the current sort order (in order to align > the sort arrows correctly), and four methods to sort > the data on a given column name. A sample of which is > shown below: > > def sortCustDataById(sortOrder = SORT_ASC) > @currentSortValue = SORT_BY_ID > if sortOrder == SORT_ASC > @listTable.clearItems() > @resultSet.sort_by { |row| row[:id] }.each do |member| > @listTable.appendItem("#{member[:id]}\t#{member[:forename]}\t#{member[:surna me]}\t#{member[:account_created]}", nil, nil, member[:id]) > end > elsif sortOrder == SORT_DESC > @listTable.clearItems() > @resultSet.sort_by { |row| row[:id] }.reverse_each do |member| > @listTable.appendItem("#{member[:id]}\t#{member[:forename]}\t#{member[:surna me]}\t#{member[:account_created]}", nil, nil, member[:id]) > end > end > end > > Ruby supposedly follows the "path of least surprise" > principle, but one thing I keep finding that disproves > this is the way I keep finding simple ways to do tasks > that would otherwise be complicated (such as the sorting) > method shown above. So what I'd like to know is: Is there > a simpler way to do what I've done above? I'd do: def sortCustDataById(sortOrder = SORT_ASC) case sortOrder when SORT_ASC sorted = @resultSet.sort { |row1,row2| row1[:id] <=> row2[:id]} when SORT_DESC sorted = @resultSet.sort { |row1,row2| row2[:id] <=> row1[:id]} else raise ArgumentError, "Illegal sort order #{sortOrder}" end @listTable.clearItems() sorted.each do |member| @listTable.appendItem("#{member[:id]}\t#{member[:forename]}\t#{member[:surna me]}\t#{member[:account_created]}", nil, nil, member[:id]) end @currentSortValue = SORT_BY_ID end This at least removes some redundant code plus it covers wrong arguments nicely and doesn't update @currentSortValue as long as the change hasn't been done. It strikes me though that you will have lots of these methods, which can be avoided by unifying the key and the sort flag: def sortCustData(sortKey, sortOrder = SORT_ASC) case sortOrder when SORT_ASC sorted = @resultSet.sort { |row1,row2| row1[sortKey] <=> row2[sortKey]} when SORT_DESC sorted = @resultSet.sort { |row1,row2| row2[sortKey] <=> row1[sortKey]} else raise ArgumentError, "Illegal sort order #{sortOrder}" end @listTable.clearItems() sorted.each do |member| @listTable.appendItem("#{member[:id]}\t#{member[:forename]}\t#{member[:surna me]}\t#{member[:account_created]}", nil, nil, member[:id]) end @currentSortValue = sortKey end You can define this method as private if you want to keep the other method as public interface. Regards robert