tags:

views:

200

answers:

5

hi guys

i had an array of objects , i need to know how should i sort my unsorted array in descending order according to the highest value inside objects

i need this using for loops not the easy way.

i had done this but it seems there is a problem

code

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

+3  A: 

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
+1  A: 
public class Student implements Comparable { ... }
    Arrays.sort(students);
    List<Object> list = Arrays.asList(students);
    Collections.reverse(list);
    students = list.toArray();
Xorty
This won't work because you need to call GetGpa() on each student object to do the comparison.
Taylor Leese
uff, yes my badbut maybe implements Comparable can save it
Xorty
Yes, that's basically what I suggested.
Taylor Leese
your fingers are little bit faster than mine :) you replied few seconds before me, but yes you are right
Xorty
+2  A: 

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
and also methods :getGpa() - nice Java;GetGpa() - C# like
Xorty
+1  A: 
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
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
A: 
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];   
                    }
        }

        }

here is the answer , thank you very much for helping me :)

Bader
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.
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.