1

I've a Kivy app that has browse functionality. When browsing files with number names, it shows it in a weird way, it kind of sorts it by "most significant bit" style. Adding a screenshot. Anyone knows how to fix it to show it in a proper order? (1,2,3... instead of 1,10,100...)

Bad image indexing...

Thanks a lot!

furas
  • 134,197
  • 12
  • 106
  • 148
Amit Small
  • 13
  • 4

1 Answers1

1

You want a natural ordering. To do this, you need to replace the function that orders the files using the sort_func property of the class kivy.uix.filechooser.FileChooserController.

An example based on the algorithm shown by @Darius Bacon in his answer to Natural Sorting algorithm:

main.py:

import re
from kivy.app import App
from kivy.properties import ObjectProperty




def natural_key(path):
    return [int(s) if s.isdigit() else s for s in re.split(r'(\d+)', path)]


def natural_sort(files, filesystem):    
    return (sorted((f for f in files if filesystem.is_dir(f)), key = natural_key) +
            sorted((f for f in files if not filesystem.is_dir(f))))


class RootWidget(FloatLayout):
    sort_func = ObjectProperty(natural_sort)



class MainApp(App):
    def build(self):
        return RootWidget()

if __name__ == '__main__':
    MainApp().run()

main.kv:

<RootWidget>:
    TabbedPanel:
        do_default_tab: False

        TabbedPanelItem:
            text: 'List View'
            BoxLayout:
                orientation: 'vertical'
                FileChooserListView:
                    sort_func: root.sort_func


        TabbedPanelItem:
            text: 'Icon View'
            BoxLayout:
                orientation: 'vertical'
                FileChooserIconView:
                    sort_func: root.sort_func

Some screenshots as an example:

enter image description here

enter image description here

FJSevilla
  • 3,733
  • 1
  • 13
  • 20