Unanswered question

Sorting in Javascript Action

I have captured all values to json which is like this:
captured_json=["2.13","1.212","1.313","13.23","20.666"]
when i try to use sort function in java script action like below:
captured_json.sort()
its throwing me an error cant find function sort in object .
I have to find an highest element in an array by sorting from highest to lowest or viceversa here in my case my desired output should be like below:
input: ["2.13","1.212","1.313","13.23","20.666"]
Desired output: ["1.212","1.313","2.13","13.23","20.666"]

shiva shanker P.
shiva shanker P.

shiva shanker P.

Level
0
3 / 100
points

Answers

Nouredine A.
Nouredine A.

Nouredine A.

Level
4
5000 / 5000
points
Team

I've tried the JS code below and it did the job.

var captured_json = ["2.13","1.212","1.313","13.23","20.666"]
var sorted = captured_json.sort(function(a, b){return a-b});
logger.debug("value sorted: "+sorted);

Neil W.
Neil W.

Neil W.

Level
4
5000 / 5000
points
Team

mine also worked

var captured_json=["2.13","1.212","1.313","13.23","20.666"]
logger.debug("sort="+captured_json.sort());

Neil W.
Neil W.

Neil W.

Level
4
5000 / 5000
points
Team

update... don't forget this is a text sort, not numbers, so doesn't quite match your desired output.

2020/11/05 08:21:26 DEBUG - neoload.JavaScript.script: sort=1.212,1.313,13.23,2.13,20.666

Nouredine A.
Nouredine A.

Nouredine A.

Level
4
5000 / 5000
points
Team

I added the function so it can sort the values as numeric values otherwise like Neil said it's a text sort.

Neil W.
Neil W.

Neil W.

Level
4
5000 / 5000
points
Team

superb, learning all the time