const sort = list => {
let currentIndex = 0;
for(let i=1; i < 3; i++) {
while(list.lastIndexOf(i) > currentIndex) {
if(list[currentIndex] > list[list.lastIndexOf(i)]) {
list[list.lastIndexOf(i)] = list[currentIndex];
list[currentIndex] = i;
}
currentIndex++;
}
}
return(list);
}
let tests = [
[3,2,1,3,2,1,3,2,1],
[3,3,3,3,2,2,1,1,1,1,1],
[2,2,1,2,1,2,3,1,2,3],
]
tests.forEach(test => console.log(sort(test)));