I'm trying to write a proof to prove that given two arrays of numbers both of size $n$, sorting them by increasing order and pairing them by index creates the matching with least cost, where cost of $(a,b)$ is $|a-b|$. In other words, I sort each array independently to get $(a_1,a_2,...,a_n)$ and $(b_1,b_2,...,b_n)$ where $a_i \leq a_j$ and $b_i\leq b_j$ when $i<j$, then pair them by index to get $(a_1,b_1)$, $(a_2,b_2)$,...,$(a_n,b_n)$.
I think a way to do this is to prove that if I take any two pairs $(a_i,b_i)$ and $(a_j,b_j)$ from the sorted arrays and swap them, i.e., resulting in $(a_i,b_j)$ and $(a_j,b_i)$, then:
$$|a_i-b_i|+|a_j-b_j| \leq |a_i-b_j|+|a_j-b_i|$$
But I'm not sure how to do the math to prove this statement. Any help appreciated.
Your answer looks good to me (+1). Here's an alternative presentation that explicitly shows the double-counted intervals in cases 2 and 3.
Case 1 ($a_i \leq a_j \leq b_i \leq b_j$): \begin{align} |a_i-b_i| + |a_j-b_j| &= (b_i-a_i)+(b_j-a_j)\\ &= (b_j-a_i)+(b_i-a_j)\\ &= |a_i-b_j|+|a_j-b_i| \end{align}
Case 2 ($a_i \leq b_i \leq a_j \leq b_j$): \begin{align} |a_i-b_i| + |a_j-b_j| &= (b_i-a_i)+(b_j-a_j) \\ &\le (b_i-a_i)+(b_j-a_j) + 2(a_j-b_i)\\ &= (b_j-a_i)+(a_j-b_i) \\ &= |a_i-b_j|+|a_j-b_i| \end{align}
Case 3 ($a_i \leq b_i \leq b_j \leq a_j$): \begin{align} |a_i-b_i| + |a_j-b_j| &= (b_i-a_i)+(a_j-b_j) \\ &\le (b_i-a_i)+(a_j-b_j) + 2(b_j-b_i)\\ &= (b_j-a_i)+(a_j-b_i) \\ &= |a_i-b_j|+|a_j-b_i| \end{align}