# Thread: Sorting in descending order

1. ## Sorting in descending order

I've got an assignment that requires me to use selection sort in descending order, and here's my code so far:
Code:
```void SortRecord( float grades[] )
{
int i, j;
int min, temp;

for( i = 0; i < (STUDENT_RECORDS - 1); i++ )
{
min = i;

for( j = (i + 1); j < STUDENT_RECORDS; j++ )
{
min = j;
}

}

for( i = 0; i < STUDENT_RECORDS; i++ )
{
}

printf( "\n" );
}```
This works grand for sorting in ascending order, but I dont know what to change to get it to go the other way.

I'm still working on grasping the flow of logic and code here, which is why I'm not simply using trial and error to solve the problem. Any help is appreciated. Thanks

2. You seem to have more variables than you need there and your overdeclaration kinda makes the code confusing. There are lots of standard sorting techniques out there, some are complex, but fast, others are simple but slow.

Here is a simple one:

Code:
```for (int x = MAX; x > 0; x--) {
for(int i = 0; i < x; i++) {
if (array[i] > array[x])
swap(array[i], array[x]);
}
}```
This kind of sort is close to what you have, you just did a little more than you need. With setups like this, switching from ascending to descending should be as simple as changing the greater than sign to a less than sign in the if statement. Also, I say this is slow, but it should be more than fast enough to accomidate your program, I'm sure.

If you want to look into sorting algorithms, the cprogramming.com has a few. For alot more detail on them, you could go to: http://eternallyconfuzzled.com/tuts/sorting.html

3. I did check out the sorting tutorials on cprogramming.com, but I found them to be very unhelpful (my comprehension level is below that of someone who will make it in the programming world, but I'ma try it anyway ).

Thanks for your help. I think I see how it works, and am going to go give it my best.

4. >I dont know what to change to get it to go the other way.
For the less sophisticated sorts, changing from ascending to descending is usually a simple matter of flipping the comparison. In your function, you change this:
Code:
`if( grades[j] < grades[min] )`
to this:
Code:
`if( grades[j] > grades[min] )`
and you're done. Though it couldn't hurt to change the variable name min to max, or you might confuse people.