how do i sort values according to a particular value using sort

Hi guys, i am currently facing a problem implementing a boolean function inside my sort function.

the story around my program is that i want to sort the values according to a civi index which is stored into the array after i compute it which is in this way.

1
2
3
4
5
6
float civiIndex=getData.getCiviIndex();
stringstream convertIndex;
convertIndex<<civiIndex;
string index=convertIndex.str();
dataStore[i]+=index+",";
copy2=dataStore[i];


i first convert it into a string then i store it into a string array.

then my logic is to sort it according to the civi index which i tried doing this
sort(dataStore,dataStore+100,greater<string>());
It works but however, it does not sort in the criteria of civi index.

then i tried to implement a boolean function, which is this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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); 
	
    
}


which returns me a 0 when i call out the result instead of a true/false.

then another question is how do i implement this function in the sort method.
i tried to do this

sort(dataStore,dataStore+100,myFunction(copy,copy));

it just gives me tons of errors.

May i know what is the correct way of sorting according to civiIndex and also the correct way of implementing and calling the sort function?

Thanks alot in advance. =)
Last edited on
You seem to be lacking some of the understanding on function pointers and the difference between a C function pointer and a C++ function pointer. I recommend that you study these in order to understand how to incorporate myFunction() into the call to std::sort().

In general, you seem to have a good general idea on how to work this out. The problem I see here is that you want to sort some dataStore collection based on an attribute that has been flattened and mixed with other data. This complicates things a big fat lot.

Instead, you should first order the data by this civiIndex, and then produce dataSotre, which will be sorted because you pre-sorted the raw data.

I don't really know how your civi index works or how is obtained, so I'll just pose an int[] example:

1
2
3
4
int myInts[] = { 1, 4, 64, 53, 14, 94 };  //Unsorted data.
//Sort the data now:
std::sort(myInts, myInts + _countof(myInts)); //_countof() defined in Visual Studio.
//Now produce your dataStore vector/array/whatever from the sorted array. 

Does this help?
oh yes it definitely helps. However i`m using ubuntu OS to write my program.
Yes that is the problem as my assignment only allows me to use a single array as i`m required to program in the OO way.

i have tried to do it just now for hours but i still can`t get it
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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); 
	
    
}


i managed to return 3 values which is 001.
which i researched 0 means false while 1 means true in boolean.
shouldn`t my values be either true or false?
why does my function return binary numbers?
and i tried to implement it in my sort, it just gives me the same errors.

PS: yes you are right,i lack understanding in pointers. I am reading on them now, i just started this subject 3 weeks ago
Topic archived. No new replies allowed.