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[:surname]}\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[:surname]}\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?
···
–
Phil Roberts | Without me its just aweso. | http://www.flatnet.net/
“Whatever you do don’t read the bible for a moral code. It advocates
predjudice, cruelty, superstition, and murder. Read it because we
need more atheists, and nothing will get you there faster than
reading the damn bible.”