# Decision Optimization

View Only

## OPL -- Array of floats does not sort correctly #### CPLEX User13 days ago #### Vincent Beraudier13 days ago #### ALEX FLEISCHER13 days ago #### CPLEX User12 days ago • #### 1.  OPL -- Array of floats does not sort correctly

Posted 13 days ago
Edited by CPLEX User 13 days ago
Hello,
Consider code:

main {
var yvalues = new Array(10);
yvalues = 500;
yvalues = -500;
yvalues = 250;
yvalues = 200;
yvalues = 200;
yvalues = 142.8;
yvalues = 142.8;
yvalues = 111.1;
yvalues = 1000;
yvalues = 1000;
// write the Values before sort
write("Before sort Values = [");
for (var i = 0; i < 10; i++) { write(yvalues[i], " "); } writeln("]");
yvalues.sort();//Sorting does not work
// write the Values after sort
write("After sort Values = [");
for (var i = 0; i < 10; i++) { write(yvalues[i], " "); } writeln("]");
}

Here, yvalues[] is hardcoded some floats. Then, I hope to sort this in ascending order via yvalues.sort(). Yet, as will be seen from the scripting log, this does not happen.

https://imgur.com/txXyHrB

where the entries, 1000 come before 111.1, for instance.

Is there a different function to sort yvalues array?

Thanks.

------------------------------
CPLEX User
------------------------------

• #### 2.  RE: OPL -- Array of floats does not sort correctly

Posted 13 days ago
Edited by Vincent Beraudier 13 days ago
The sort() method sorts the values as strings in alphabetical and ascending order.

Because of this, the sort() method will produce an incorrect result when sorting numbers.

In order to sort numbers, you need to pass a comparison function to sort:

function c(a,b){

if (a == b) return 0;

if (a < b) return -1;

return 1;

}

yvalues.sort(c);

will produce the correct array.

------------------------------
Vincent Beraudier
------------------------------

• #### 3.  RE: OPL -- Array of floats does not sort correctly

Posted 13 days ago
Hi,

I shared a full example at https://github.com/AlexFleischerParis/oplscripting/blob/main/arraymethods.mod

``````execute
{

var ar=new Array(4);
ar=2;
ar=10;
ar=4;
ar=1;
ar=7;

writeln("length = ",ar.length);

writeln("join = ",ar.join(" , "));

ar.reverse();
writeln("reverse = ",ar.join(" , "));

ar.sort();
writeln("sort default (string) = ",ar.join(" , "));

function numberorder(a,b){ return ((a==b)?(0):((a<b)?-1:1)); }

ar.sort(numberorder);
writeln("sort number = ",ar.join(" , "));
}
/*

which gives

length = 5
join = 2 , 10 , 4 , 1 , 7
reverse = 7 , 1 , 4 , 10 , 2
sort default (string) = 1 , 10 , 2 , 4 , 7
sort number = 1 , 2 , 4 , 7 , 10

*/​``````

------------------------------
[Alex] [Fleischer]
[EMEA CPLEX Optimization Technical Sales]
[IBM]
------------------------------

• #### 4.  RE: OPL -- Array of floats does not sort correctly

Posted 12 days ago
Edited by CPLEX User 12 days ago
Hi Alex,

In your example, why do you not issue ar.end() when ar goes out of scope?

In my OPL code, following the examples in CPLEX documentation, I carefully issue cplex.end(), for instance, where previously I have instantiated it via a new such as cplex = new IloCplex() to avoid memory leakage.

When I tried ar.end() at my end, OPL complains that end is an undefined method. Perhaps .end() give one the end of array position like in STL iterators?

Like in C/C++, is there no need to issue ar.end() to clean up the allocated memory? Is there some other method that cleans up the allocated memory?

------------------------------
CPLEX User
------------------------------

• #### 5.  RE: OPL -- Array of floats does not sort correctly

Posted 12 days ago
arrays are javascript objects and will be destroyed by the javascript garbage collector

regards

------------------------------
[Alex] [Fleischer]
[EMEA CPLEX Optimization Technical Sales]
[IBM]
------------------------------