novelWriter icon indicating copy to clipboard operation
novelWriter copied to clipboard

Writing Statistics: incorrect column sorting

Open HeyMyian opened this issue 2 years ago • 3 comments

Sorting by the columns "Idle" (percentage) or "Words" produces incorrect sorting. The columns are sorted by the first digit of their respective value, not by the complete value. See the two attached screenshots.

Related: Sorting by Histogram does nothing -- but should conceivably simply do the same as sorting by word count.

Sorting by idle percentage. 5% is sorted between 54% and 49%. 2022-04-08___22_28_00

Sorting by word count. It sorts from starting digit 9 down to starting digit 1, also ignoring the negative sign. 2022-04-08___22_28_18

HeyMyian avatar Apr 08 '22 20:04 HeyMyian

This is not a bug. The column sorting is a text sort, and the widget doesn't support any other sorting mechanism. That's why the dates are in ISO format.

In order to fix this, I need to make a custom implementation of the item widgets, which is very slow in Python. I can test it out though and check if it is too slow.

vkbo avatar Apr 09 '22 07:04 vkbo

This is actually far easier then re-implementing the whole widget

# Untested Example
class WSTreeWidgetItem( QtGui.QTreeWidgetItem ):
    def __init__(self, parent=None):
        QtGui.QTreeWidgetItem.__init__(self, parent)

    def __lt__(self, otherItem):
        column = self.treeWidget().sortColumn()
        # sort based on column; ie.
        if column == 0:  # ISO Date
             return self.text(column) > otherItem.text(column)
        if column > 0 and column < 3:  # Numerical Columns
            try:
                return float( self.text(column) ) > float( otherItem.text(column) )
            except ValueError:
                return self.text(column) > otherItem.text(column)
        else:  # histogram
            try:
                return float( self.text(3) ) > float( otherItem.text(3) )
            except ValueError:
                return self.text(column) > otherItem.text(column)

then use something like this WSTreeWidgetItem in place of QTreeWidgetItem in the _updateListBox method

Ryex avatar Jul 13 '22 01:07 Ryex

Yes, this solution is what I mean by "a custom item widget". I haven't tested it yet, but thanks for the example. :)

vkbo avatar Jul 13 '22 10:07 vkbo