Hello Everyone,
I hope you guys are doing well! Also it’s late, but Merry Christmas and a Happy New Year!
So today I found a great method for reordering. It was inspired by one of the client called Trung. It’s for exercises but can apply for other apps too!
Here’s a video that I made for Trung and you guys can check it too! : reorder in glide on Vimeo
Here's the JS Codes
1) csv_reorder
function replaceNumbers(csv, selectedNumber) {
var numbers = csv.split(',').map(Number);
var selectedIndex = numbers.indexOf(selectedNumber);
if (selectedIndex !== -1 && selectedIndex > 0) {
[numbers[selectedIndex], numbers[selectedIndex - 1]] = [numbers[selectedIndex - 1], numbers[selectedIndex]];
csv = numbers.join(',');
}
return csv;
}
var originalCSV = p1;
var selectedNumber = p2;
var updatedCSV = replaceNumbers(originalCSV, selectedNumber);
return(updatedCSV); // Log the updated CSV string
2) csv_reorder copy
function replaceNumbersOpposite(csv, selectedNumber) {
var numbers = csv.split(',').map(Number);
var selectedIndex = numbers.indexOf(selectedNumber);
if (selectedIndex !== -1 && selectedIndex < numbers.length - 1) {
[numbers[selectedIndex], numbers[selectedIndex + 1]] = [numbers[selectedIndex + 1], numbers[selectedIndex]];
csv = numbers.join(',');
}
return csv;
}
var originalCSV = p1;
var selectedNumber = p2;
var updatedCSV = replaceNumbersOpposite(originalCSV, selectedNumber);
return(updatedCSV);
I hope this helps and let me know if there’s something wrong or if there are improvements!
Thank you and Have a great day!