SortTabs icon indicating copy to clipboard operation
SortTabs copied to clipboard

Numeric sort instead of string sort

Open elmimmo opened this issue 12 years ago • 5 comments

Please add an option to sort numerically, where 2 comes before 10, ideally ignoring file extension (to prevent the extension from also spoiling the numeric sort).

elmimmo avatar Jul 23 '13 08:07 elmimmo

Not sure it's a good idea to always sort this way, I will think about...

In the meantime, you can do it yourself by creating a new plugin containing something like:

import sort_tabs
from itertools import groupby


class SortTabsByNameNumericalCommand(sort_tabs.SortTabsByNameCommand):
    '''Sort Tabs by file name (numerical)'''
    sorting_indexes = (1, 3, 2)

    def fill_list_views(self, list_views):
        super(SortTabsByNameNumericalCommand, self).fill_list_views(list_views)

        for item in list_views:
            key = []
            for digit, g in groupby(item[2], lambda x: x.isdigit()):
                val = "".join(g)
                if digit:
                    key.append(int(val))
                else:
                    key.append(val)
            item.append(key)

bizoo avatar Aug 16 '13 09:08 bizoo

I did not suggest to always sort this way, but to have a preference that one could switch on or off (although I confess I most probably would have it always on).

elmimmo avatar Aug 16 '13 21:08 elmimmo

Does the plugin in my previous comment works as you expected ?

bizoo avatar Aug 19 '13 13:08 bizoo

Works like wonders. Thanks!

elmimmo avatar Aug 24 '13 06:08 elmimmo

The code doesn't work anymore in ST3, you must use:

from SortTabs import sort_tabs
from itertools import groupby


class SortTabsByNameNumericalCommand(sort_tabs.SortTabsByNameCommand):
    '''Sort Tabs by file name (numerical)'''
    sorting_indexes = (1, 3, 2)

    def fill_list_views(self, list_views):
        super(SortTabsByNameNumericalCommand, self).fill_list_views(list_views)

        for item in list_views:
            key = []
            for digit, g in groupby(item[2], lambda x: x.isdigit()):
                val = "".join(g)
                if digit:
                    key.append("{0:>99}".format(val))
                else:
                    key.append(val)
            item.append(key)

bizoo avatar Dec 30 '13 10:12 bizoo