in reply to Returning an array in a specific way for csv

I tried to play around with the sort of the data array

%data is not an array. Rather, it is a hash which is quite a different structure and does not retain a natural order. See the FAQ entry How do I sort a hash (optionally by value instead of key)?

The rest of your question as stated is not the clearest and so it would be pure guesswork to try to propose a solution. Can you be more specific?