let's say you have a struct/class of this kind:
struct A
{
int number ;
double value ;
char name[30] ;
};
and you want to sort an array of A s on the member value .
first, write a comparison function that the sort can use which accepts two A s to be compared:
bool compare_on_descending_value( const A& first, const A& second )
{
// function to compare elements of the sequence
// return true if first should appear *before* second
// in the sorted sequence; false otherwise
// for example, this would sort on descending value (member)
return first.value > second.value ;
}
then call std::sort (header <algorithm> ) passing the comparison function to tell it how elements should be compared for the sort:
void sort_on_descending_value( A* array, std::size_t num_elements )
{
// use std::sort from <algorithm>
std::sort( array, array+num_elements, compare_on_descending_value ) ;
}
My attempts to implement Vijayan121's solution is generating
incomprehensible (to me) compiler errors. Let me show you how
I tried to implement the solution, which should isolate my mistakes.
1. This is the class I am using, which is included as a header in the
main program:
class LakeDataClass
{
public:
char DTG[13];
float DepthOfMeasurements;
char WaveDirectionStrength[5];
char Weather[9];
float DissolvedOxygen;
float pH;
float AirTempCelsius;
float WaterTempCelsius;
float WaterTempFahrenheit;
float AirTempFahrenheit;
float SunLight;
float BarPress;
};
Initial attempts to sort on the character array DTG failed so
badly I switched to the relatively simple float var 'pH'.
2. The class is then used to instantiate copies of itself into a global
array (LakeDataArray), and a temporary holder for an individual object/class
('entry') is initialized with zeroes.
LakeDataClass * LakeDataArray[13];
LakeDataClass entry = {0};
The user chooses how many objects he wants to create, and the program then
successfully gathers data from him and writes each object as a binary object
into a disk file. All of this has been confirmed to work fine.
3. Now I want to sort the data in the disk file, and this is where I have
encountered problems I can't solve. Here is how I have implemented Vijayan121's
solution:
a. First, PROTOTYPE the two functions before main():
bool compare_on_descending_value(const LakeDataClass& first,
const LakeDataClass& second);
void sort_on_descending_value(const LakeDataClass * array, int);
b. Second, DEFINE the two functions after main():
bool compare_on_descending_value( const LakeDataClass& first,
const LakeDataClass& second )
{
return first.pH > second.pH;
}
void sort_on_descending_value( const LakeDataClass *, sizeof(LakeDataClass->DTG))
{
std::sort (LakeDataArray, LakeDataArray+ObjectReadCount, compare_on_descending_value);
}
c. And third, CALL the function from within main():
sort_on_descending_value(LakeDataClass * LakeDataArray, sizeof(LakeDataArray[]->DTG,
compare_on_descending_value( LakeDataClass& first, LakeDataClass& second ));
I will not include the compiler errors, because reviewers might be able
in one look to see what mistake(s) I am making. I appreciate any help that can be
offered. Wiglaf.