Use ARRAY_SORT to sort one array and apply the same order to the other. For example, if you have array1 and array2:
array1: [4, 1, 3, 2]
array2: [Z, X, Y, R]
SELECT
ARRAY_SORT(x, y -> y, ARRAY_AGG(array2), ARRAY_AGG(array1)) AS sorted_array2
FROM your_table;
This ensures the order in array1 is applied to both arrays, maintaining their alignment.
Output:
sorted_array1: [1, 2, 3, 4]
sorted_array2: [X, R, Y, Z]
For more information, check our documentation.