Trying to descipher mystical custom UiList sorting API

Hi! I have a UiList, the items on the UiList have a name and a date.
I’m trying to sort them by date, the list of indices I get after sorting seems to be correct.

However the UiList seems to be ignoring these indices, or the dates are not displaying properly when drawing.

class MY_UL_LIST(bpy.types.UIList):
    date_sort: bpy.props.BoolProperty(name="Date Sort", default=True)

    def draw_item(
        self, context, layout, data, item, icon, active_data, active_propname, index, flt_flag
    ):
        row = layout.row(align=True)
        row.prop(item, "display_name", text="", emboss=False)
        row.prop(item, "date", text="", emboss=False)

    def draw_filter(self, context, layout):
        row = layout.row(align=True)
        icon = "SORT_DESC" if self.use_filter_sort_reverse else "SORT_ASC"
        row.prop(self, "use_filter_sort_reverse", text="", toggle=True, icon=icon)
        row.prop(self, "date_sort", text="", toggle=True, icon="TIME")

    def filter_items(self, context, data, propname):
        items = getattr(data, propname)

        if self.date_sort:
            items_sorted = sorted(
                range(len(items)),
                key=lambda i: datetime.strptime(items[i].get("date"), r"%m/%d/%Y %H:%M:%S"),
            )

            print("Blender sorting is: ", items_sorted)
        else:
            items_sorted = sorted(
                range(len(items)),
                key=lambda i: items[i].get("display_name"),
            )

        filter_flags = [self.bitflag_filter_item] * len(items_sorted)
        return filter_flags, items_sorted

packed.blend (82.5 KB)

Any idea what’s going on? Attached is a blend file where you can test this issue

Thanks

The list will be ordered by index in the panel. If you are always pulling from a fixed data set like in your example sorting before adding into the collection should work fine.

packed

packed.blend (112.2 KB)