Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 How to sort in descending order?
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
HazySmoke)345




PostPosted: Tue Aug 07, 2007 7:41 pm   Post subject: How to sort in descending order?

By default, sort() sorts everthing in ascending order. That's not what I want. First, I tried this:
code:
#include <cstdio>
#include <algorithm>
using namespace std;

int comp(int a, int b){
        return b-a;
}

int main(){
        int a[] = {3,6,1,7,1,7,4,2,5,2}, i;
        sort(a, a+10, comp);
        for (i = 0; i < 10; ++i) printf ("%d ", a[i]);
return 0;}


The output turned out to be: 2 5 2 4 7 1 7 1 6 3 - not very appealing.
Then I tried this:

code:
#include <cstdio>
#include <algorithm>
using namespace std;

int main(){
        int a[] = {3,6,1,7,1,7,4,2,5,2}, i;
        sort(a, a+10, operator>);
        for (i = 0; i < 10; ++i) printf ("%d ", a[i]);
return 0;}


The compiler gaved me an error...
testing.cpp:7: error: no matching function for call to `sort(int[10], int*, <unknown type>)'

Apparantly it doesn't recognize what 'operator>' is.

What am I doing wrong?
Sponsor
Sponsor
Sponsor
sponsor
PaulButler




PostPosted: Tue Aug 07, 2007 8:39 pm   Post subject: RE:How to sort in descending order?

c++:

int comp(int a, int b){
   return b-a;
}


Shouldn't this be:

c++:

int comp(int a, int b){
   return b < a;
}


(or b > a?)
HazySmoke)345




PostPosted: Tue Aug 07, 2007 9:16 pm   Post subject: Re: How to sort in descending order?

Thanks, you're absolutely right. I guess the compare function works differently in C++ than in C. 8 bits.

I still have no clue why wouldn't operator> work, though. Doesn't C++ treat it like any other functions?
OneOffDriveByPoster




PostPosted: Thu Aug 09, 2007 9:20 am   Post subject: Re: How to sort in descending order?

You can try std::greater<T> in <functional>.
HazySmoke)345 @ Tue Aug 07, 2007 9:16 pm wrote:
I still have no clue why wouldn't operator> work, though. Doesn't C++ treat it like any other functions?
Yes, but you aren't dealing with operator> when you do ( 0 > 0 ). You cannot do operator>(0, 0).
PaulButler




PostPosted: Fri Aug 10, 2007 7:46 am   Post subject: RE:How to sort in descending order?

Is there not a way to make an infix operator into a function? In OCaml you just wrap it in brackets (eg., 5 + 3 is the same as (+) 5 3). Maybe C++ has something similar?
wtd




PostPosted: Fri Aug 10, 2007 11:06 am   Post subject: RE:How to sort in descending order?

O'Caml also lacks operator overloading, which adds some complexity to the issue.

The way one does this in C++ is to create a function object.

code:
template <typename T>
struct gt
{
    bool operator()(T a, T b)
    {
        return a > b;
    }
};
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: