Copy keys array before sorting so as not to affect original order.