2

I am trying to wrap my head around a section in my project. I have a structure array made up of the usual variables. (First & Last Name, ID, User Name, Test Scores, Average & Letter Grade)

I have to sort by grade (which isn't hard) but then it also looks that I would have to sort by ID number from lowest to highest.

Kinda at a loss on how to proceed. Any tips would greatly be appreciated!

dmatthews1
  • 21
  • 1

1 Answers1

3

First sort by ID number using std::sort and then sort by grade using std::stable_sort. This way the array will be sorted by grade and among students with the same grade it will be sorted by ID.

Another maybe simpler way (but much less cool) is to just implement a comparison function that first checks grade and then ID:

if (a.grade == b.grade)
  return a.id < b.id;
return a.grade < b.grade;
Benjy Kessler
  • 7,356
  • 6
  • 41
  • 69