Numeric Sorting issue [SOLVED]

I have a list that I would like to sort, but it's not sorting the way I expected (even though I see what's happening)

I have a list with different dB values like this (after using Filter > Sort)
-14.5dB
-23dB
-8.6dB

So KM is looking at the first number and sorts it, so 1, 2, 8, instead of looking at 14, 23, 8, and putting the 8 at the top

Is there a way to go around this?

I could find a way to add a 0 before each value that only contains a single digit using RegEx, for example, but I wonder if this is the only way?

The values are being sorted as text, by the look of the data you have given. If you can extract the numeric part of the data and convert/store as numbers, that should help.

Hopefully this may point you to a way forward.

1 Like

So this is what I came up with and it's working
add zero to single digit values.kmmacros (25 KB)

1 Like

I see what you mean and it makes sense, it is seeing this as text, not numbers.
I think saving that as numbers would be a more complex process (at least for me).

I just shared my macro to add the 0 to the beginning. It's working.
Thanks for the tip. I didn't think about it that way

1 Like

An alternative approach would be to use a one line shell script which makes use of the SORT command. In your above example I notice you are ignoring the "-" signs and sorting by the absolute value of the numbers in ascending order. This should achieve the same thing, without the leading zero:

Screenshot 2024-03-05 at 10.15.08

If you want the sort order reversed, remove the "-r" from the end of the command.

2 Likes

and an Execute JavaScript for Automation variant:

Numeric sort.kmmacros (2.3 KB)

To reverse the order of the sort, edit to:

.sort((a, b) => b - a)

in lieu of

.sort((a, b) => a - b)
2 Likes