using a boolean function to sort an array in ascending order

Hi all, i am having trouble in sorting my array in ascending order according to the area calculated. However, i have succeeded in sorting it in descending order based on one of the codes that was given to me by a kind soul.
May i know how do i sort it in ascending order? i`m sure its similar to the function below. I have tried to reverse the greater than sign but it doesn`t work. Can someone advise me on this?

Thanks alot in advance =)


1
2
3
4
5
6
7
8
9
10
struct my_function  
{
    bool operator() ( const string& a, const string& b ) const
   {
        float convertA = 0, convertB = 0 ;
        std::stringstream(a) >> convertA ;
        std::stringstream(b) >> convertB ;
        return convertA  > convertB ;
   }
};
Change your operator function by substituting

return convertA > convertB ;

for

return convertA < convertB ;
@vlad

i tried to use convertA < convertB
However, the arrays do not sort or rather they do not print out.
You should decide whether you need to sort it ot to print.:)
@vlad

yes i do need to sort the array then print it out.

this is what i have done

1
2
3
4
5
6
sort(arrayOfShapes,arrayOfShapes+100,my_function2());
 for(int i=0;i<counter;i++)
{
cout<<arrayOfShapes[i]<<endl;

}


However, when i use my previous code. It sorts fine and prints out fine.
however when i use convertA < convertB. It doesn`t print out
Try to print the array before the sorting and look what will be printed. Also it is not clear what is the value of counter? For example you can substitute it for your magic number 100 and print your array.:)
i tried it. the counter is 0
the strange thing is, the array prints out fine before sorting.
However after sorting, it just prints out blank.


edit: here`s the output


Before48,cross,ws,1,1@1,2@2,3@3,4@
Before5,square,ws,1,2@2,3@3,4@4,4@
After
After
Last edited on
If the counter is equal to zero then what do you want shall be printed?
i understand what you are trying to say. i tried implementing counter++ below but still nothing prints and the array doesn`t sort
Topic archived. No new replies allowed.