Build and sort array