cancel
Showing results for 
Search instead for 
Did you mean: 

Analytics Application - Array.sort numerically

Alexander_Blasl
Participant
0 Kudos

Hi,

how can I sort my Array numerically? Array.sort(); did it like this:

Best regards

Alex

Accepted Solutions (1)

Accepted Solutions (1)

shraddha_mundwadkar
Participant

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

SAC analytical application - SORT issue | SAP Community

Answers (2)

Answers (2)

N1kh1l
Active Contributor

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

Alexander_Blasl
Participant
0 Kudos

Thx. Your code + shraddha_mundwadkar Code in combination was the key.

0 Kudos

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.

N1kh1l
Active Contributor

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

0 Kudos

Thanks : ) !

0 Kudos

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);