 # List sort by sting length then value

Hi, I’m trying to sort a bunch of sheet numbers, by sheet string number length then by alaphabet and number.

eg.
A1
A2
A3
R1
R2
A101
A102
A103
A104
RC21
RC22
RC23
S101
S102
S103
A1001
A1002
A1003
A1004

You’ll probably have to take certain aspects of your sheet numbers and use List.SortByKey. For example, if you take the first character of each sheet number, you can use List.SortByKey (or even List.GroupByKey to isolate the sheets for each discipline. Then, you can group each of the sheets within a given discipline using the result from a List.Count node. Subsequently, you can sort each of the final groups using the numerical values. For example, given the following list:
`['A1', 'A2', 'A10', 'A20', 'B1', 'B2', 'B10', 'B20']`
first group the sheet numbers into two lists by A and B:
`['A1', 'A2', 'A10', 'A20']`
`['B1', 'B2', 'B10', 'B20']`
If you isolate the numbers using a String.Substring node, you can then get the following lists for each list of sheet numbers:
`['1', '2', '10', '20']`
`['1', '2', '10', '20']`
You can then get the length of each string and subdivide it further into other sublists:

``````[['1', '2'],
['10', '20']]
[['1', '2'],
['10', '20']]
``````

You can then sort each of the discipline-specific lists using the numbers as keys (you may have to cast them to doubles for them to sort properly, but I’m not sure). You should eventually end up with a list of sheet numbers like this:

``````[[['A1', 'A2'],
['A10', 'A20']],
[['B1', 'B2'],
['B10', 'B20']]]``````

Check this, Orchid may help.

2 Likes
2 Likes

Thanks. I think this is making the most sense but I’m not sure I can get my element values to match the keys at the end. I dont think itll be sorting the elements the same at the end.

This didn’t actually work either. Need to list sort sheet number before combining the sheet number and name. Which means I need to sort the sheet name to the sheet number somehow

You’ll have to do multiple List.GroupByKey operations. The first will group your sheet numbers by their length, the second will group each of the resultant groups by their prefixes (I believe Clockwork has some regex nodes. The regex pattern here would be ‘\w+’ to get one or more word characters in the sheet number). After you isolate by length and prefix, you can then use a List.SortByKey node to get the lists sorted properly within their groups. It is important that you use a List.SortByKey rather than List.Sort since List.SortByKey will allow you to sort the actual Sheets based on their Sheet Numbers rather than based on whichever arbitrary value List.Sort decides to use. This is essentially what Akli has already explained, but their example shows the Sheet Numbers being sorted using List.Sort. The order of the Sheet Numbers is ultimately correct, but it doesn’t order the actual sheets yet–that’s where List.SortByKey comes in.

@vanman
once the names are sorted,
it is easy to retrieve the corresponding sheets

1 Like

Thanks man, But whats the key I would sort by after grouping by length? Keep getting this annoying layout for the sheet list. Get the first character of the string (using String.Substring) then sort by that.

sort sheets 2.dyn (16.7 KB)

If you use the length of the sheet number concatenated with the sheet number itself, sorting by the newly created key achieves the desired result. Also, you can remove the List.Shuffle in both graphs. I just included to make sure I wasn’t getting false positives.

1 Like

I think I cracked it but the UI.Listview Data seems to be doing its own things  hey @Mostafa_El_Ayoubi. Does your UI.listview Data node do its own sorting?

Hi @vanman,
If you set the “sortitems_optional” input to false it won’t

1 Like