A simpler way to do this?

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.”

Phil Roberts wrote:

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.

So what I’d like to know is: Is there
a simpler way to do what I’ve done above?

I’m not sure if I have a simpler way of doing what you have done, but
you might want to take a look at http://www.netpromi.com/listview.html.

I subclassed FXTable to provide a lot of the functionality you are
looking for. It’s probably not any more efficient than yours, but it
might give you another view of how to solve the problem.

Jamey

Confidentiality Notice: This email message, including any attachments, is for the sole use of the intended recipient(s) and may contain confidential and/or privileged information. If you are not the intended recipient(s), you are hereby notified that any dissemination, unauthorized review, use, disclosure or distribution of this email and any materials contained in any attachments is prohibited. If you receive this message in error, or are not the intended recipient(s), please immediately notify the sender by email and destroy all copies of the original message, including attachments.

“Phil Roberts” philrob@HOLYflatnetSHIT.net 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