Alphanumeric string sort order in workshop - object table

I am trying to make a default sort order on a column that contains a alphanumeric string.

Normal sorting doesn´t work like i want. The result is:
ABC-1-24
ABC-10-24
ABC-2-23
ABC-2-24

What i want is (sort by the last number first, and then the first number):
ABC-2-23
ABC-1-24
ABC-2-24
ABC-10-24

What is the best way to solve this?

I made a function that seems to do the trick, but how do i implement it?

function sortStringsChronologically(strings: string[]): string[] {

    return strings.sort((a, b) => {

        const partsA = a.split("-");
        const partsB = b.split("-");

        const midA = parseInt(partsA[1], 10);
        const midB = parseInt(partsB[1], 10);

        const lastA = parseInt(partsA[2], 10);
        const lastB = parseInt(partsB[2], 10);

        if(lastA !== lastB) {
            return lastA - lastB;
        }

        if(midA !== midB) {
            return midA - midB;
        }

        return lastA - lastB;

    });

}

const strings = ["ABC-1-24", "ABC-1-23", "ABC-3-24", "ABC-10-24", "ABC-2-24"];
const sortedStrings = sortStringsChronologically(strings);

console.log(sortedStrings);

[LOG]: ["ABC-1-23", "ABC-1-24", "ABC-2-24", "ABC-3-24", "ABC-10-24"] 

1 Like

If you can enable it, I would recommend creating a custom column where the same data could be naturally sorted. Eg: ABC-1-23ABC-23-01

Thanks!
I tried this now and it works for the sorting part, but now i have a column in the object table that i don´t want the user seeing becuase it can become confusing with two simular ID:s.

There is a decision to be made here about which attribute gets to stay or be visible. In the ontology, it’s possible to mark a property as hidden and it’s also possible to disable sort under render hints. However I’m not sure how that reflects in workshop once that’s done.

In the object table’s configuration pane, could you achieve the desired result by deleting the new column from the “Columns” section and adding it to the “Default Sort(s)” section?

If i delete it, it´s no longer sortable. Probably because it´s a function-backed column, and not a property from the ontology.