• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 469
  • Last Modified:

Using STL to sort a vector

I am trying to sort the STL vector Record with the given sort statement.  The vector is reordered thoough not as I expected, nor does my

    cout << szColName << endl;

statement seem to execute.  How can I sort this vector?



#include <algorithm>
#include <functional>
#include <vector>
using namespace std ;

class Record
{
public:
  UCHAR szQualifier [STR_LEN];
  UCHAR szOwner [STR_LEN];
  UCHAR szTableName [STR_LEN];
  UCHAR szTableNameX [STR_LEN];
  UCHAR szColName [STR_LEN];
  UCHAR szTypeName [STR_LEN];
  UCHAR szRemarks [REM_LEN];
  SWORD DataType;
  SWORD Scale;
  SWORD Radix;
  SWORD Nullable;
  SDWORD Precision, Length;
  int Record::operator > (const Record& obj)
  {
    cout << szColName << endl;
    return strcmp((char*)szColName,(char*)obj.szColName);

  }
};


  sort(theVector.begin(),theVector.end(),greater<Record*>());
0
Kalvyn
Asked:
Kalvyn
  • 4
  • 3
  • 2
1 Solution
 
KalvynAuthor Commented:
I forgot the definition...

typedef vector <Record*> Vector;
Vector recs;
Vector::iterator Iterator;
0
 
KalvynAuthor Commented:
opps...
typedef vector <Record*> Vector;
Vector theVector;
Vector::iterator Iterator;

sort(theVector.begin(),theVector.end(),greater<Record*>());
0
 
bcladdCommented:
greater<Record *> should be the give away: You are comparing the pointers, not the records themselves. You need a comparison function that takes two Record pointers  and compares the Records.

-bcl
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
efnCommented:
Hi Kalvyn,

The third argument to sort is telling it to sort by pointer values.  So it is sorting the records by their addresses, not by their contents, and it's not calling your > operator, which is why you don't see any output.

Try greater<Record>() instead of greater<Record*>().

--efn
0
 
efnCommented:
My answer would have made sense if theVector were a vector of Records, but since it's a vector of pointers to records, bcladd's answer is right and my suggestion to try greater<Record>() was bogus.  Sorry I didn't read the code more carefully.
0
 
KalvynAuthor Commented:
So how do I define the comparison function to sort by szColName and call it with sort?
0
 
bcladdCommented:
typedef Record * RecordPtr
bool lessThan(const RecordPtr & x, const RecordPtr & y)
{
  return strcmp((char *)x->szColName, (char *)y->szColName) < 0);
}

Modify direction of > or < to suit your needs.

-bcl

0
 
bcladdCommented:
Oh, and

sort(theVector.begin(), theVecotor.end(), lessThan);

-bcl
0
 
KalvynAuthor Commented:
Thank bcl,

With a slight syntax change, it worked for me.

typedef Record * RecordPtr;
bool lessThan(const RecordPtr & x, const RecordPtr & y)
{
 return (strcmp((char *)x->szColName, (char *)y->szColName) < 0);
}
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now