Note that (this is more to OP) the OP said he wanted (or just expected) @order to contain 2,3,4,1,6,5 but in this solution (which i believe is correct) @order contains 3,0,1,2,5,4 ... Note especially the 1-based vs 0-based difference which is necessary for the array slice to work.