Behavior of the SORT function

Post your questions and help other users.

Moderator: Martin

Post Reply
rcfree
Posts: 43
Joined: 21 Mar 2013 16:51

Behavior of the SORT function

Post by rcfree » 11 Dec 2016 15:14

Hi,

I noticed that the luck function has behavior that I consider strange, because when I apply the function to sort a list of numbers the following occurs:

Case A ====================================================== =========

List before applying the SORT function (with numbers containing "."):
1,245.00
98.72
2450.78
87,25
4199.99
3509.99
3.509.99
3,314.99
3899.99

After applying the SORT function:

1,245.00
3,314.99
3.509.99
87,25
98.72
2450.78
3509.99
3899.99
4199.99

Case B ===================================================== ========

List before applying the SORT function (without numbers containing "."):
1245.00
98.72
2450.78
87,25
4199.99
3509.99
3509.99
3314.99
3899.99

After applying the SORT function:
87,25
98.72
1245.00
2450.78
3314.99
3509.99
3509.99
3899.99
4199.99


Looking at this behavior I came to the conclusion that the SORT function is not able to work with numbers in the format 0,000.00 (dotted number), it seems that it considers the number 1,245.00 as 1.25, since it considers 1,245.00 less than 87,25.

In Case B everything happens as expected.

Is this behavior of the function correct?


Thanks!









 1

User avatar
MURTUMA
Posts: 697
Joined: 05 Mar 2013 22:43

Re: Behavior of the SORT function

Post by MURTUMA » 11 Dec 2016 22:12

While it could be porrible to code the fuction to be able to handle such things, I think it is not a good idea. You're basically sorting numbers which are formatted differently from each others. It is an edge case and "fixing" the function might cause unforseeable effects in other cases.

What is the difference between XX.XX and XX,XX or X,XXX.XX and X.XXX.XX. You should look your problem from a different angle. Here it is different formats, so the solution would be to first format the numbers alike and only then to sort them.

User avatar
Martin
Posts: 4434
Joined: 09 Nov 2012 14:23

Re: Behavior of the SORT function

Post by Martin » 13 Dec 2016 20:33

The sort function sorts values alphabetically but tries to improve the sorting when text is mixed with numbers. For example:
a 1
a 3
a 20

instead of the regular :
a 1
a 20
a 3

Single quotes and periods are considered regular text.

You could avoid this "natural" sorting mode by using the sort function with three parameters sort(list, casesensitive, natural) so a function call would look like sort(list, false, false). However, this will not really solve your sorting issue since alphabetical order is not much better. You could first convert all numbers to format XXXX.XX and then sort the numbers.

Regards,
Martin

rcfree
Posts: 43
Joined: 21 Mar 2013 16:51

Re: Behavior of the SORT function

Post by rcfree » 14 Dec 2016 10:56

Thanks for the clarification and guidance.

houdinix64
Posts: 33
Joined: 08 Mar 2013 12:45

Re: Behavior of the SORT function

Post by houdinix64 » 14 Dec 2016 17:22

is it possible to sort in alphabetical order all keys of a particular map? I cant find any clue in help documentation

example:
1. trigger= shortcut
2.action=script:
sort("{global_autoprofile_map_wifi_dbase}")
3.action=input dialog (single choice)
{global_autoprofile_map_wifi_dbase ,listformat,comma}


thanks..

bogdyro
Posts: 239
Joined: 04 Apr 2015 15:14

Re: Behavior of the SORT function

Post by bogdyro » 14 Dec 2016 19:55

Tricky. Don't know if there's a simpler way.
Used a default map to test.

map=getFlowStatisticsDuration();

lst=newList();
for (key in getMapKeys(map))
addElement(lst,key); //put the keys into a list
lst=sort(lst); //sort the list
new_map=newMap(); //future sorted map
for (element in lst)
addMapEntry(new_map, element, map[element]) //add the sorted keys and the value that corresponds to each key

houdinix64
Posts: 33
Joined: 08 Mar 2013 12:45

Re: Behavior of the SORT function

Post by houdinix64 » 15 Dec 2016 16:00

Thanks..100% working.. :D

Post Reply