# CJam, 19 bytes {S+m!{S/1fb:-z}$0=} This is an anonymous function that pops an array of integers from the stack and returns an array of integers separated by a space. Try it online in the [CJam interpreter][1]. ### How it works S+ e# Append a space to the array of integers. m! e# Push the array of all possible permutations. { e# Sort the array by the following: S/ e# Split the array at the space. 1fb e# Add the integers in each chunk (using base 1 conversion). :-z e# Push the absolute difference of both sums. }$ e# Permutations with a lower absolute difference will come first. 0= e# Select the first permutation. [1]: http://cjam.aditsu.net/#code=%5B1%202%203%204%5D%20%20%20%20%20%20%20%20%20%20%20e%23%20Input%0A%7BS%2Bm!%7BS%2F1fb%3A-z%7D%240%3D%7D%20e%23%20Function%0A~p%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20e%23%20Call%20and%20print.