# [SOLVED] how do i reverse this method for sorting?

• 03-08-2009, 10:06 PM
PureAwesomeness
[SOLVED] how do i reverse this method for sorting?
this method sorts numbers from smallest to biggest
and i want it to be the opposite from biggest to smallest
i tried to exchange places with the first and second for loop, but that doesn't seem to work.
doesnt anybody have any suggestion?

Code:

```public class SelectionSort {   /** The method for sorting the numbers */   public static void selectionSort(double[] list) {     for (int i = list.length - 1; i >= 1; i--) {       // Find the maximum in the list[0..i]       double currentMax = list[0];       int currentMaxIndex = 0;       for (int j = 1; j <= i; j++) {         if (currentMax < list[j]) {           currentMax = list[j];           currentMaxIndex = j;         }       }       // Swap list[i] with list[currentMaxIndex] if necessary;       if (currentMaxIndex != i) {         list[currentMaxIndex] = list[i];         list[i] = currentMax;       }     }   } }```
• 03-08-2009, 10:23 PM
emceenugget
whenever you're comparing for max/min, swap it for the opposite. or you can just reverse the resulting list.
• 03-08-2009, 10:32 PM
PureAwesomeness
Quote:

Originally Posted by emceenugget
whenever you're comparing for max/min, swap it for the opposite. or you can just reverse the resulting list.

Hey! i never thought of that, reverse the result list.
Thank You so much, i had this in my head for hours!
• 03-08-2009, 10:37 PM
PureAwesomeness
if i want to change the codes to do the opposite i need to change this?
Code:

``` if (currentMaxIndex != i) {         list[currentMaxIndex] = list[i];         list[i] = currentMax;```
how do i change the i though?