[PyKDE] Custom sorting function for the QListView widget

Troy Melhase troy at gci.net
Sun Jul 4 00:20:00 BST 2004


>     def compare(self, item, column, ascending):
>         try:
>             a = int(str(self.text(column)))
>         except (ValueError, ):
>             a = self.text(column)
>         try:
>             b = int(str(item.text(column)))
>         except:
>             b = item.text(column)
>         return cmp(a, b)
>

Actually, this should be a smidge better:

    def compare(self, item, column, ascending):
        a = self.text(column)
        b = item.text(column)
        try:
            a = int(str(a))
        except (ValueError, ):
            pass
        try:
            b = int(str(b))
        except:
            pass
        return cmp(a, b)

With this one, you'll only pay for the call to text() once, even if the value 
can't be converted to an int.

-- 
Troy Melhase, troy at gci.net
--
I have sworn upon the altar of God eternal hostility against every form of 
tyranny over the mind of man. - Thomas Jefferson




More information about the PyQt mailing list