how to implement a boolean function in a sort algorithm

Hi guys, i know i have posted this many times. However, i am still facing problems sorting my string array.


the story is i am supposed to sort my string array in accordance to civiIndex.
which in this case, civiIndex was originally a float value and i converted it to string using stringstream.


then i tried to use
sort<[array-name],[array-name]+size,greater<string>());

However, it sorts in descending order in alphabetical order. Not in accordance
to civiIndex.

then i tried to implement a boolean function into my sort

which is this.


bool pointTwoD::myFunction(string a ,string b)
{

dataA=a;
dataB=b;

float convertA,convertB;
stringstream(dataA)>>convertA;
stringstream(dataB)>>convertB;
//cout<<convertA;
//cout<<convertB;

return (convertA>convertB);


}



here i tried to convert the string values into float and compare them using their float values. However, they returned me a value of 001.

May i know the solution for this?
I have been thinking on it for 2 days and to avail.
Pleas help me =(
Either
1
2
3
4
5
6
7
8
9
10
struct my_function
{
    bool operator() ( const std::string& a, const std::string& b ) const
   {
        float convertA = 0, convertB = 0 ;
        std::stringstream(a) >> convertA ;
        std::stringstream(b) >> convertB ;
        return convertA  > convertB ;
   } 
};

and then:
std::sort( [array-name], [array-name]+size, my_function() ) ;


Or:
1
2
3
4
5
6
7
    bool my_function( const std::string& a, const std::string& b )
   {
        float convertA = 0, convertB = 0 ;
        std::stringstream(a) >> convertA ;
        std::stringstream(b) >> convertB ;
        return convertA  > convertB ;
   } 

and then:
std::sort( [array-name], [array-name]+size, my_function ) ;
hi JL, i have tried the method, but it asks me for string inputs.

if i want to use the strings in my array as a comparison. How should i go about doing it?

can i do it like this?

string getCopy=dataStore[i];

and then initialize the string variables as a and b

Edit: i get tons of errors too =(
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
struct my_function
{
    bool operator() ( const std::string& a, const std::string& b ) const
   {
        float convertA = 0, convertB = 0 ;
        std::stringstream(a) >> convertA ;
        std::stringstream(b) >> convertB ;
        return convertA  > convertB ;
   }
};

int main()
{
    std::string a[5] = { "1.67", "7.83", "3.97", "8.07", "5.32" } ;
    for( const auto& s : a ) std::cout << s << ' ' ; std::cout << '\n' ;

    std::sort( a, a+5, my_function() ) ;
    for( const auto& s : a ) std::cout << s << ' ' ; std::cout << '\n' ;
}

Output:
1.67 7.83 3.97 8.07 5.32
8.07 7.83 5.32 3.97 1.67

i have tested your code. it works on my program but it doesn`t sort.

Could it be because i stored an alphabet in my array?

Just print out the contents of your array before and after the call to std::sort() - that will tell you what's going on.
i tried to troubleshoot, but it still doesn`t sort.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
for(int i=0;i<counter;i++)
{
	cout<<"before"+dataStore[i];

}	
	counter++;
	counter--;	
		
	sort(dataStore,dataStore+100,my_function());
	for(int i=0;i<counter;i++)
	{
	 cout<<"After"+dataStore[i];
		
	}
	counter++;
	counter--;


output

before2,3,K,23,23,1,2,36.11,before2,3,M,55,55,1,2,75.35,After2,3,K,23,23,1,2,36.11,After2,3,M,55,55,1,2,75.35,

edit: i have no idea why is it no sorting at all
Last edited on
You have two elements:

2,3,K,23,23,1,2,36.11 and 2,3,M,55,55,1,2,75.35

With the comparison function

1
2
3
4
5
6
7
    bool my_function( const std::string& a, const std::string& b )
   {
        float convertA = 0, convertB = 0 ;
        std::stringstream(a) >> convertA ;
        std::stringstream(b) >> convertB ;
        return convertA  > convertB ;
   } 


The conversion stops at the comma, and you end up comparing 2.0 to 2.0, which means these two elements are equal so they're already in order.
Ok thank you. I figured out the logic to trial and error. Correct me if i`m wrong, if i want to compare the values of 36.11 and 75.35. I would just need to store them at the first value and they would compare as per the sort? +)
That would work.
Thanks alot for your help guys. I have solved the issue and my program is now working fine! =)
Topic archived. No new replies allowed.