I'm going to assume that you either haven't learned about vectors yet, or that you are trying to figure this out without using vectors. Anyway, using the vector function sort() would be a better alternative in this case.