I want to automate a test case, where I need to sort items in descending order by their numeric values. For sorting user story list items might change. In start they look from the beginning it looks at this values. I used hash for this
var values = $$('div.vertical-list-container div.list-group-item');
var numberHash = { 'One': 1, 'Two': 2, 'Three': 3, 'Four': 4, 'Five': 5, 'Six': 6, 'Seven': 7, 'Eight': 8, 'Nine': 9, 'Ten': 10 };
After I created the loop where I collected all these elements.
for(var i=0;i<values.length;i++)
{
numberArray.push(numberHash[values[i].getText()])
}
So now loops looks this [1,2,3,4,5,6] After that, I used bubble sort to make descending order
function bubble_Sort(a)
{
var swapp;
var n = a.length-1;
var x=a;
do {
swapp = false;
for (var i=0; i < n; i++)
{
if (x[i] < x[i+1])
{
var temp = x[i];
x[i] = x[i+1];
x[i+1] = temp;
swapp = true;
}
}
n--;
} while (swapp);
return x;
}
bubble_Sort(numberArray);
And in the last loop, I have a problem.
for(var i = 0; i < values.length; i++){
var elem = values[i];
var elemNumber = numberHash[values[i].getText()];
var index = numberArray.findIndex((element) => element == elemNumber);
var target = values[index];
elem.dragAndDrop(target);
}
All values changed positions except 2. I know why it is( because after .dragAndDrop function value 1 goes below 6, and now value 2 is in zero position, but loop start check next position. So now I have ordered values in this order => this[6,2,5,4,3,1] instead of [6,5,4,3,2,1]. Can someone know how to fix it? Where is the problem in the loop? I really need to this :(