on 05-31-2022 11:13 AM
Hi,
Try below syntax for Ascending sort
for (var i = 0; i < Array.length; i++) {
var target = Array[i];
for (var j = i - 1; j >= 0 && (Array[j] > target); j--) {
Array[j+1] = Array[j];
}
Array[j+1] = target
}
console.log(Array);
Looks like the sorting on measures is missing. Please see below conversation
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Alex,
The reason its sorting this way as the elements in your array are treated as characters (see the quotes on the element). When filling this array try converting the elements to a integer first. Once converted to numbers the sort function will give you the desired result.
Use parseInt to convert the elements to number from string.
something like below
var Array2 = ArrayUtils.create(Type.number); //Array2 to store converted Array
for (var j=0; j < Array1.length; j++) { //Array1 is your original Array with characters
Array2.push(Number.parseInt(Array1[j]));
}
Array2.sort();
console.log(Array2);
I tested with a small sample and got the desired results as below
Regards
Nikhil
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello folks:
Does not work for me, sadly, see:
it seems that an array of type Integer or Number will not sort properly using the sort() function
-> [1, 6, 3, 12] will be, incorrectly sorted as [1, 12, 3, 6] as if it were an Array of String, which it ain't.
Instead I expect: [1, 3, 6, 12]
nikhil.anand : If you tried your code with ["33", "1", "6", "3", "12"] you will experience the bug in the sort() API.
Hey Andreas,
You are right. JS sort behavior is like that. You can use a code snippet to achieve the sort
// For Ascending Sort Array 2 is my Array
for (var i = 0; i < Array2.length; i++) {
var target = Array2[i];
for (var j = i - 1; j >= 0 && (Array2[j] > target); j--) {
Array2[j+1] = Array2[j];
}
Array2[j+1] = target
}
console.log(Array2);
// For Descending sort
for (var i = 0; i < Array2.length; i++) {
var target = Array2[i];
for (var j = i - 1; j >= 0 && (Array2[j] < target); j--) {
Array2[j+1] = Array2[j];
}
Array2[j+1] = target
}
console.log(Array2);
Below is the out put with your values.
Regards
Nikhil
what will work in Analytics Designer, if you wanted to sort an Array of data type Integer/Number is the following code:
// Example how to sort an Array of Integer (that is 0 1,2,3,...)
// For Numbers intead ofIntegers, that is .., -99,-98, -97, .., -2, -1, 0, 1, 2, 3,... <br>// the code needs to be adapted to identify the empty elements
<br>var t1 = ["33","1","6","3","12"];console.log (t1);<br>var Array2 = ArrayUtils.create(Type.integer); //Array2 to store converted Array t1
<br>for (var j=0; j < t1.length; j++) { // Array1 is your original Array with characters<br> var help = ConvertUtils.stringToInteger (t1[j]);<br> Array2[help] = help; // Brute force "sorting"<br>}
<br>console.log ("sorted, but bloated by many empty elements"); console.log (Array2);<br>console.log ("length:"); console.log (Array2.length);
// Now removing those empty elements == compacting the bloated Array2
var Array3 = ArrayUtils.create(Type.integer); //Array3 to store compacted Array2
<br>for ( j=0; j < Array2.length; j++) { <br> <br> help = Array2[j];<br> <br> if (help >= 0) { // Note, this only works for integers <br> Array3.push (help);<br> }<br> else {<br> //skip, because this is an empty element<br> }<br>}<br><br>console.log ("sorted and compacted:"); console.log (Array3);<br>console.log ("length:"); console.log (Array3.length);
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
85 | |
10 | |
10 | |
9 | |
7 | |
6 | |
5 | |
5 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.