What I'd do is declare a struct consisting of a member that stores the line and another member that stores the average value. Then I can populate an array of these struct objects and sort it according to the average value, after which I can print it by printing the lines.