I could see several approaches, but the basic idea is always to consider an 'item' to sort not being one line, but one group. A straightforward (not necessarily the best) approach would be to build an auxiliary array, where each array holds one group. For efficiency reason (i.e. if you have to sort more than just a few groups), you might consider extracting the number which is used to be sorted, when building the array. For instance, your aux array could look like this:

This is a very good question! Sorry that I didn't clarify it in my first post. I want to sort the first column by group. For example, if the input is like this (I just changed the value of column 2 for the line of "Amy")

I think, the idea of this sorting is, that each group (a group being all lines with the same name) has one "pivot" number, which is the smallest number within this group. The groups then need to be sorted ascending according to their pivot number. Within each group, the lines are simply sorted ascending to their numbers.