www.digitalmars.com Home | Search | C & C++ | D | DMDScript | News Groups | index | prev | next
Archives

D Programming
D
D.gnu
digitalmars.D
digitalmars.D.bugs
digitalmars.D.dtl
digitalmars.D.dwt
digitalmars.D.announce
digitalmars.D.learn
digitalmars.D.debugger

C/C++ Programming
c++
c++.announce
c++.atl
c++.beta
c++.chat
c++.command-line
c++.dos
c++.dos.16-bits
c++.dos.32-bits
c++.idde
c++.mfc
c++.rtl
c++.stl
c++.stl.hp
c++.stl.port
c++.stl.sgi
c++.stlsoft
c++.windows
c++.windows.16-bits
c++.windows.32-bits
c++.wxwindows

digitalmars.empire
digitalmars.DMDScript
electronics



c++ - qsort/compare

↑ ↓ ← Paul <wcount12 aol.com> writes:
Hello, I need to sort an array of structures into desending order
by an element declared as a float.  Here is my compare function
which im using with Qsort.
----------------------------------------------------------------
int compare_one(const void *v1, const void *v2)
{
    int return_value;

    return_value = (*(struct proform*)v1).eq_rating - (*(struct
proform*)v2).eq_rating;

    if(return_value < 0 )
    {
       return 28;
    }
    else if( return_value > 0 )
    {
       return -34;
    }
    else
    {
       return 0;
    }

    return return_value;
}
-----------------------------------------------------------

This is sorting as if the variables are int's rather than floats,
so the frational part is being ignored.

Question:  How do sort on a float variable this is part of a
structure?

Thanks

Paul
Mar 18 2007
↑ ↓ Bertel Brander <bertel post4.tele.dk> writes:
Paul skrev:
 Hello, I need to sort an array of structures into desending order
 by an element declared as a float.  Here is my compare function
 which im using with Qsort.

[Snip code]
 This is sorting as if the variables are int's rather than floats,
 so the frational part is being ignored.
 
 Question:  How do sort on a float variable this is part of a
 structure?

int compare_one(const void *v1, const void *v2) { double return_value = (*(struct proform*)v1).eq_rating - (*(struct proform*)v2).eq_rating; if(return_value < 0 ) { return 1; } else if( return_value > 0 ) { return -1; } return 0; } -- Just another homepage: http://damb.dk But it's mine - Bertel
Mar 18 2007
↑ ↓ → Paul <wcount12 aol.com> writes:
Hi Bertel, thanks your code works fine.

Kind regards

Paul
Mar 18 2007