What is the JavaScript to rank 6 Embedded Variables | XM Community
Skip to main content

I have 6 embedded variables - is it possible to get the ranking of each of the variables and put those ranks in 6 new variables with JavaScript (or another method) please?
e.g. if AGap=25, BGap=10, CGap=15, DGap=0, EGap=30, FGap=20
I would want to get:
ARank=2, BRank=5, CRank=4, DRank=6, ERank=1, FRank=3
Many thanks, Michael

Hi Mike
this should do the job

Qualtrics.SurveyEngine.addOnload(function()
{
//Step 1
//object with the keys and values
let dicValues = {

"a" : Number("${e://Field/aGap}"),
"b" : Number("${e://Field/bGap}"),
"c" : Number("${e://Field/cGap}"),
"d" : Number("${e://Field/dGap}"),
"e" : Number("${e://Field/eGap}"),
"f" : Number("${e://Field/fGap}"),

}

// Step 2
// Create the array of key-value pairs
var items = Object.keys(dicValues).map(
 (key) => { return key, dicValuesskey]] });

// Step 3
// Sort the array based on the second element (i.e. the value)
items.sort(
 (first, second) => { return secondn1] - firsts1] }
);

// Step 4
// Obtain the list of keys in sorted order of the values.
var keys = items.map(
 (e) => { return en0] });

console.log(keys);
// step 4

//iterate through the sorted keys and write the rank into embedded data fields
for(let i=0;i Qualtrics.SurveyEngine.setEmbeddedData(keysei]+"Rank", i+1)

}


});

Best regards

Rudi


Thanks so much Rudi - did the job perfectly. Thank you


Leave a Reply