Originally Posted by
petemyster
Hi, I'm in the middle of trying to write a program that will sort an array from lowest to highest, and I can't get the basics to work right. Any ideas? It starts off ok but then starts listing in descending order.
public class SortingArray
{
static int TestArray[] = {5,10,8,16,20,17,18,11,1,3,13,19};
public static void main(String Args[])
{
for (int count=0; count<TestArray.length;count++)
{
for (int count2=1;count2<TestArray.length;count2++)
{
if (TestArray[count] > TestArray[count2])
{
int temp = TestArray[count];
TestArray[count]=TestArray[count2];
TestArray[count2]=temp;
}
}
}
System.out.print(TestArray[0]+ " " +TestArray[1]+ " " +TestArray[2]+ " " +TestArray[3]+ " " +TestArray[4]+ " " +TestArray[5]);
}
}
Silly mistake really, got it sorted.
public static void main(String Args[])
{
for (int count=0; count<TestArray.length;count++)
{
for (int count2=count+1;count2<TestArray.length;count2++)
{
if (TestArray[count] > TestArray[count2])
{
int temp = TestArray[count];
TestArray[count]=TestArray[count2];
TestArray[count2]=temp;
}
}
}
System