Lists sort by 1.1., 1.2. , 2.,10., ...instead of 1., 10., 2.,

Hello!

I want to sort a list from 1.1., 1.2. , 2.,10 given by some lists…

I’m trying to sort a list by part of the sublist value in this case the first column wich has numbers in it.
However if i use SortByKey it transforms my numbers into text and it sorts like 1., 10., 2.,…
I think it is because of the ‘’.’’ but i really need it to be ‘’.’’ and sorted like 1.1., 1.2. , 2.,10

Any suggestion would be appreciated!

Hello,

you try somethink like this:

Hi,

using that “list.Sort” it has the same problem

Hey,

The problem you’ve got initially is that while 6.4 is a number, 6.4.1 isn’t (numbers don’t have more than 1 decimal)… It is probably text, you can use a Type node to check. You also have values like 2.2+2.3
image

So ideally you’d need a string sorting node which can handle your complexity, unfortunately I’m not aware of one…

Otherwise we’re dealing with converting the number bits to numbers and trying to sort sequently by each “.”

Unfortunately I’m not that bright so i’m trying to work it out…

You’ll need to sequentially sort at level? I’m not sure the best way to do that… This is where I’m at right now…

I’ll keep trying…It’s also more complex because your values vary in length…

Hope that helps,

Mark

3 Likes

@Mark.Ackerley , If I didn’t use the “+” like in 2.2+2.3, can it be easier?
I can use another solution for that and it stays like the others with just one value.

Thanks!

UPDATE

I’ve tried in a smaller file and it worked with a python Script that i’ve found here in the Forum (Sorting a list of numbers in a logical order - #2 by JacobSmall).

It works until i join that script to “List.SortByKey” then the sort changes again.

@JI_NGS try this:

Hey,

Yeah, i think it does need to be sequential… 1st sort by 1st index, then group by 2nd index and sort each group by the 2nd index and then the 3rd, then the 4th…

If you just remove the “.” and sort, then if you have 1.30.1 (1301) it will be put infront of 1.3.31? (1331)

It’s knotty :slight_smile:

EDIT : unless we pad? that might work!

Does this work for you? You can always pad by more numbers?

@Mark.Ackerley Nope, it will end up 1.30.1 because I am inserting after the first character then the last;
Issues might happen if there is only one dot, such as 3.1 for example, but it can be solved with an If statement I guess.

I think in yours, the 3 has become a 30? I might be wrong :slight_smile: Your idea was definitely the right one…

If I didn’t use the “+” like in 2.2+2.3, can it be easier?

Yes I would say that consistancy is going to make your life easier :slight_smile:

Hope that helps,

Mark

EDIT : no, mine fails with numbers with fewer decimals! @EdsonMatt has the answer :slight_smile:

1 Like

You’ve sorted your items using the Python node, they don’t need to be sorted again. You just need to know on which index they are now, because it has changed. To do that, use List.IndexOf, this will give you the sorted indexes and then use List.GetItemAtIndex to get your main list items in the correct order. Basically, something like:

My thoughts about this:
When you have a certain data ranging from 1 to 9 before the first “.” (such as “1.1.1, 2.1, 3.1”), the sort function works fine. However, when mixing those with items whose first digit is greater than 9, before the first “.” (1.1, 10.1, 2.1, 11.5), the sort function will not work like you expect, of course, talking about strings here. This problem happened to one of your examples.

So, you can try to sort and get the items in your main list in this way:

first_itm = DSCore.List.FirstItem(DSCore.String.Split(x,".")<1>);
len = DSCore.String.Length(first_itm);
group = DSCore.List.GroupByKey(x,len)["groups"];
sorted = DSCore.List.Flatten(DSCore.List.Sort(group<1>));
idx = DSCore .List.IndexOf(x,sorted);
get_itms = DSCore.List.GetItemAtIndex(y,idx);
4 Likes

@Mark.Ackerley Ah yes, I see your point.

1 Like

@EdsonMatt has the best solution :slight_smile:

Here in nodes for those of us who get lost in design script!

2 Likes

@EdsonMatt, it worked!

I used like this (in a larger file)

Many many thanks!!

1 Like

Hi @JI_NGS (and the others),

Have you heard about “Natural Sorting”?

For example check out this code:

2 Likes