How can I sort a data frame by groups?
The 'dplyr' package in R is ideal for these types of data manipulation tasks. The arrange function for example can group a dataframe by a certain column, and then sort by another column. For example:

arrange(df, desc(mileage), group_by = year)


See arrange for documentation on the arrange function, and dplyr for the dplyr package description.