How to sort a text list separated by new row in a fixed order by text-string?

Hi Community,

I work with product lists and use different tools to create them in make. The result currently is text list like this incl quantities in a random order (depending the input):

12x Product A
4x Product D
3x Product C
2x Product B

Since these are supposed to be picklists in a warehouse, I want to provide a fixed sorting order by name like this:

Product A
Product B
Product C
Product D

So the list output should be like this:

12x Product A
2x Product B
3x Product C
4x Product D

There is no alphabetical logic and no sorting by quantities. Instead, I want to sort by product names in a given order.

Can someone help me accomplish that with make?

Honestly an easy solution here is to use AI. I threw it into groq which is a free ai and it did it for me.

Prompt:
Since these are supposed to be picklists in a warehouse, I want to provide a fixed sorting order by name like this:

Product A
Product B
Product C
Product D

So an example would be
So the list output should be like this:
12x Product A
2x Product B
3x Product C
4x Product D

Do it for this list and only return the sorted list in order and nothing else
{{1.text}}

Blue print with prompt and result. blueprint (27).json (7.7 KB)

1 Like

Thanks. That is what I’m doing so far. This works great in 95% of all cases, but when it comes to numbers, somehow even GPT4o does make errors and starts hallucinating numbers. This is why I’m looking into a better solution without AI.

I solved this by using the Custom JS plugin to execute the following JS code:

function sortWarehouseItems(unsortedProducts) {
    const fixedOrder = [
        "Product A",
        "Product B",
        "Product C"
    ];

    const items = unsortedProducts.trim().split(/\s(?=\d+x)/).map(line => {
        const [quantity, ...nameParts] = line.trim().split(' ');
        const name = nameParts.join(' ');
        return { quantity, name };
    });

    items.sort((a, b) => {
        return fixedOrder.indexOf(a.name) - fixedOrder.indexOf(b.name);
    });

    const sortedItems = items.map(item => `${item.quantity} ${item.name}`);
    return sortedItems.join('\n');
}

// Call the function with the input object
const sortedOutput = sortWarehouseItems(input.unsortedProducts);
console.log(sortedOutput);

// If you want to return sortedOutput from the function, you can do so in a larger context where this code is used
return sortedOutput;

Input variable “unsortedProducts” can be filled from previous node in make.

4 Likes