1

I have an unsorted array of objects. I need to know how I can sort my array in descending order, according to the highest value inside the objects.

I need to do this using for loops, not the easy way.

I had done this but it seems there is a problem:

student[] temp=new student[s.length];

for (int i=0;i<s.length;i++)
{
    if (s[i].GetGpa() > s[i + 1].GetGpa())
    {
        temp[i] = s[i];
    }
}

How should I do it using for loops?

mwfearnley
  • 3,303
  • 2
  • 34
  • 35
Bader
  • 207
  • 1
  • 8
  • 21
  • Seems like this is a homework assignment - Bader needs to do it the hard way, without built-in sorts. @Bader: get a book on sort algorithms. – Skip Head Apr 18 '10 at 18:31
  • @Bader - What is "the easy way"?. If this is homework you should tag it as such. – Taylor Leese Apr 18 '10 at 18:34
  • Duplicates: http://stackoverflow.com/questions/1694751/java-array-sort-descending http://stackoverflow.com/questions/1946668/sorting-using-comparator-descending-order-user-defined-classes – Jørn Schou-Rode Apr 18 '10 at 18:39

5 Answers5

3

This should get you started. You'll need to create your own Comparator and then call Collections.Sort().

Collections.sort(List<T> list, Comparator<? super T> c)
Taylor Leese
  • 51,004
  • 28
  • 112
  • 141
2

I suggest looking at the Wikipedia article for sorting algorithms. Your code fails because you compare each element only with the next one - but that's not a sorting algorithm at all, because in order to be correctly placed in the first position, an element needs to be bigger than all other elements, not just the next one.

Also, Using a lowercase class name is very much against Java coding standards.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
1
public class Student implements Comparable { ... }
    Arrays.sort(students);
    List<Object> list = Arrays.asList(students);
    Collections.reverse(list);
    students = list.toArray();
Xorty
  • 18,367
  • 27
  • 104
  • 155
1
for (int j=0;j<s.length;j++) {
    for (int i=0;i<s.length - 1 - j;i++)
    {
        if (s[i].GetGpa() > s[i + 1].GetGpa())
        {
            student temp = s[i];
            s[i] = s[i+1];
            s[i+1] = temp;
        }
    }
}
Chris J
  • 9,164
  • 7
  • 40
  • 39
  • there is a problem come when i put this code Exception in thread "main" java.lang.NullPointerException at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source) at java.lang.Double.parseDouble(Unknown Source) at main_function.main(main_function.java:35) – Bader Apr 18 '10 at 19:52
0
for(int i=0;i<s.length;i++)
{
    for(int j=i+1;j<s.length;j++)
    {
        if(s[j].GetGpa()>s[i].GetGpa())
        {
            student[] temp=new student[5];
            temp[j]=s[j];
            s[j]=s[i];
            s[i]=temp[j];
        }
    }
}
Termininja
  • 6,620
  • 12
  • 48
  • 49
Bader
  • 207
  • 1
  • 8
  • 21
  • 1
    Note that `new student[5]` will create a fixed array. Attempting to use an index beyond 4 (0-4 is 5 values) will cause an `Exception`. http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html – James P. Apr 18 '10 at 23:05
  • Also here's an applet that demonstrates the different sorting algorithms: http://www.cs.oswego.edu/~mohammad/classes/csc241/samples/sort/Sort2-E.html – James P. Apr 18 '10 at 23:07