Sort alphabetically entries in cells

Hi KNIMErs,

I have a column with entries separated by a separator, and the order of entries is random. Sample cell content:
White, Black, Red

I wonder how to change the order of entries to resort them alphabetically. Expected output:
Black, Red, White

Any suggestions?
Happy KNIMEing,
Kaz

Hi @Kazimierz

If the words you need to sort are not repeated in the same cell, you could use a -cell splitter- node with “create a set” as splitting option with comma as separator. This should sort the words within the cells in a set format. Then you can use a -rename column- node to convert the column set type back to string type. This should do the trick.

Hope this helps.

Best
Ael

1 Like

Hi @aworker

Thank you for your suggestion. I’ve tested that on the following sample dataset
image

with the following configuration of Cell Splitter node

and setting column type to String for newly created column however decors are not set in the alphabetical order afterwards
image

Am I doing anything incorrectly?
Happy KNIMEing,
Kaz

Hi @aworker and All

The challenge is solved now. I’ve applied unpivoting-sorting-pivoting sequence to get closer to expected results
image
with Cell Splitter output set to ‘As new columns’.
Pivoting node generates the following outcome


Then, after using Joiner, Column Filter, Column Rename, and String Manipulations nodes I’m getting the following result
image

Happy KNIMEing to All KNIMErs,
Kaz

For the record, the route that @aworker had in mind works if you put a Column Expression in between and use the arraySort() function. That saves you quite a few nodes.

image

3 Likes

Hi @ArjenEX

For the record, could you please show the correct configuration of Column Expression node then?
Best regards,
Kaz

Sure, it’s just the default function.

3 Likes

Hi @ArjenEX
It really works.
Thank you so much,
Kaz

1 Like

I had an alternative 4 node solution

I preferred it to a solution involving “Sorter”, simply because Sorter is going to sort ALL of your rows, so you would need to take care to ensure you don’t reorder your data where you don’t want to.

I was disappointed though at not being able to find a way to improve on @ArjenEX’s Column Expressions solution :wink: and so…

… here is my trusty friend String Manipulation with a single node solution, using a java hack… but only because otherwise @gonhaddock will read this and be disappointed :rofl:

string(
   java.util.Arrays.stream($Item List$.split(",")).sorted()
	.collect(java.util.stream.Collectors.joining(","))
	)

It was @ArjenEX 's use of arraySort() in Column Expressions that made me think of it. Yes it’s another undocumented “java hack”, but I find it fun :slight_smile:
Sorting comma separated list.knwf (18.2 KB)

linking to Fun with String Manipulation

5 Likes

Thank you @takbb
This shows there are various possible solutions for KNIME challenges and each solution might be the best one. Depending on the needs :slight_smile:

1 Like

hi @Kazimierz , yes you are right that there are often many ways to achieve the result.

Glad you got what you needed. As I believe that this is the type of small task that might occasionally come up again, I decided to turn the String Manipulation into a component that allows you to specify the delimiter (both original and new), and also reverse the sort order. This time I used the String Manipulation (multi column), so that it could be applied to more than one column at once.

If you are interested, it can be found here.

2 Likes

Thank you @takbb for going the extra mile and exceeding the expectations!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.