Sorting integer arrays

I need to sort an array made of integers, but the javascript array.sort treats all values as text.

The javascript scripts which work in browsers don’t seem to function in Servoy.

Is there a simple way to get an integers array sorted without writing a method to do that?

The javascript sort method sorts alfabetically unless a sorting funtion is used.
Try this:

  1. create a global method numsort:```
    return arguments[0] - arguments[1]
2. pass it to the sort method:

var list = new Array(1,3,2,11,21)
list = list.sort(globals.numsort)


Rob

Rob,

thanks for your reply. Your method replicates the function I tried in a browser but I couldn’t find a simple way to pass the array values to the method in Servoy.

It works, but I don’t understand the innner logic of this function. Probably I should study javascript :oops:

rioba

You should indeed try to get more knowledge about JavaScript :)

But here is what basically happens:```
return arguments[0] - arguments[1]

arguments[0] is the first value used in the method
arguments[1] is the second value used in the method

These values are 2 sequential values in the array.

The returned result is either negative or positive and influences the sorting order.

Basically the sorting will perform a loop where values trade places based upon the positive or negative value

Marcel,

thank you: your reply clarifies some of my perplexities. But what I still don’t fully understand is how parameters are passed to the sorting method. Usually you have to explicity do that using method(param1,param2…). In this case the parameter is the sorting method, so I imagine that the values are passed to the method by the inner logic of the javascript sort function. Not so intuitive. Maybe this has something to do with the lack of explicity declared pointers in javascript, something I have not yet fully elaborated.

THis is indeed one of those things that are dealt with by the engine. Servoy hooks into that with this method.