Frank Rueter | OHUfx
2018-11-14 02:28:33 UTC
Hi,
I need to use the QSortFilterProxyModel to sort strings with numbers in
them, i.e. perform natural sorting.
I found this snippet online:
|import re def atoi(text): return int(text) if text.isdigit() else text
def natural_keys(text): return [atoi(c) for c in re.split('(\d+)',
text)] l = ['Item1', 'Item2', 'Item3', 'Item10', 'Item20']
l.sort(key=natural_keys) print l |
This works nicely but Iâm wondering if itâs reliable and efficient to
implement it like this in the QSortFilterProxyModel.lessThan() method?!
|def lessThan(self, source_left, source_right): natural_keys(source_left)
< natural_keys(source_right) |
Any thoughts on this?
Note: I need to avoid 3rd party libraries.
Cheers,
frank
â
--
ohufxLogo 50x50 <http://www.ohufx.com>
*vfx compositing <http://ohufx.com/compositing.html> | *workflow
customisation and consulting <http://ohufx.com/customising.html>* *
*<http://ohufx.com/compositing.html>*
<http://www.nukepedia.com/nubridge>
Your gateway to over 1,000 free tools... right inside of Nuke
<http://www.nukepedia.com/nubridge>
I need to use the QSortFilterProxyModel to sort strings with numbers in
them, i.e. perform natural sorting.
I found this snippet online:
|import re def atoi(text): return int(text) if text.isdigit() else text
def natural_keys(text): return [atoi(c) for c in re.split('(\d+)',
text)] l = ['Item1', 'Item2', 'Item3', 'Item10', 'Item20']
l.sort(key=natural_keys) print l |
This works nicely but Iâm wondering if itâs reliable and efficient to
implement it like this in the QSortFilterProxyModel.lessThan() method?!
|def lessThan(self, source_left, source_right): natural_keys(source_left)
< natural_keys(source_right) |
Any thoughts on this?
Note: I need to avoid 3rd party libraries.
Cheers,
frank
â
--
ohufxLogo 50x50 <http://www.ohufx.com>
*vfx compositing <http://ohufx.com/compositing.html> | *workflow
customisation and consulting <http://ohufx.com/customising.html>* *
*<http://ohufx.com/compositing.html>*
<http://www.nukepedia.com/nubridge>
Your gateway to over 1,000 free tools... right inside of Nuke
<http://www.nukepedia.com/nubridge>