[Interest] efficient natural sorting

Frank Rueter | OHUfx frank at ohufx.com
Wed Nov 14 03:28:33 CET 2018


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>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20181114/ada8b467/attachment.html>


More information about the Interest mailing list