sort vector of objects

Oct 25, 2009 at 3:44am
I am trying to sort a vector list of objects of type stockType. Sorting with the following does not get me the desired result, not sure what it is sorting on (i expected this):

void stockListType::sortStockList(void)
{
sort(list.begin(), list.end());
}

So i tried the following in an attempt to sort on a specific member of the object.

bool stockListType::mySortFunc(const stockType& a, const stockType& b)
{
return (a.getnoOfShares() < b.getnoOfShares());
}

void stockListType::sortStockList(void)
{
sort(list.begin(), list.end(),&stockListType::mySortFunc);
}

This i cannot get to compile, from everything i am reading, i think i am on the right track, however cannot figure it out. Any help would be greatly appreciated.
Oct 25, 2009 at 4:21am
What you are trying to do is possible, but it requires a bit of magic boiler-plate to do it.

The way that the STL expects you to do it is by overloading the < operator for the elements of the list. Here's a simple example to get you going.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

// Here's my element type: a point in space.
struct point_t
  {
  int x, y;
  point_t( int x = 0, int y = 0 ):
    x( x ),
    y( y )
    { }
  bool sortfunc( const point_t& lhs, const point_t& rhs ) const
    {
    return (lhs.x == rhs.x)
         ? (lhs.y <  rhs.y)
         : (lhs.x <  rhs.x);
    }
  };

// Here's my comparitor for the sort() function to use.
bool operator < ( const point_t& lhs, const point_t& rhs )
  {
  return lhs.sortfunc( lhs, rhs );
  }

// The rest of this stuff is just to display a point...
ostream& operator << ( ostream& outs, const point_t& point )
  {
  return outs << "(" << point.x << "," << point.y << ")";
  }

// ...and a list of points
ostream& operator << ( ostream& outs, const vector <point_t> & points )
  {
  for (unsigned n = 0;;)
    {
    outs << points[ n ];
    if (++n >= points.size()) break;
    outs << ", ";
    }
  return outs;
  }

// Enjoy!
int main()
  {
  vector <point_t> v;

  v.push_back( point_t( 10, -7 ) );
  v.push_back( point_t( -2, 14 ) );
  v.push_back( point_t(  3,  5 ) );
  v.push_back( point_t(  3,  0 ) );
  v.push_back( point_t(  3, 78 ) );
  v.push_back( point_t( 24, 42 ) );

  cout << "v = " << v << endl;

  cout << "sorting v..." << flush;
  sort( v.begin(), v.end() );
  cout << "done.\n";

  cout << "v = " << v << endl;

  return 0;
  }

If you are not permitted to overload the < operator, you can make your sort function just a non-class function or functor that you can pass to the sort() algorithm.

If you are not permitted either alternative, let me know with the additional information of whether or not you can make your 'mySortFunc' static.

Hope this helps.

[edit] Added some commentary to the code.
Last edited on Oct 25, 2009 at 4:25am
Oct 26, 2009 at 9:06pm
i am not sure i understand what is going on here. I am a little bit confused as to how your sort( v.begin(), v.end() ); is working differently then mine?

FYI, C++ is new to me. I am a embedded programmer and normally do not have to deal with this stuff, but school is making me break down and learn it. The D.S. Malik books are not very helpful :)

Oct 26, 2009 at 11:26pm
Maybe not, but the C++ documentation is an invaluable help.

http://www.cplusplus.com/reference/algorithm/sort/
The elements are compared using operator< for the first version, and comp for the second.

And, starting with line 22 above:
1
2
3
4
5
// Here's my comparitor for the sort() function to use.
bool operator < ( const point_t& lhs, const point_t& rhs )
  {
  return lhs.sortfunc( lhs, rhs );
  }

It will do you no harm to learn C++. ;-)
Oct 27, 2009 at 7:21pm
You could simplify things by not defining the sortFunc member. Instead, define the operator< as a member function of the struct itself. It's simpler and more natural in my mind. The key thing to understand is that in order to sort elements within a vector or any kind of array there must be a way of comparing each element of the array with another. The functionality for comparing built in types is built in. For any user defined types, you must define your own method. It also helps to have a basic understanding of predicates. The std::sort uses the std::less predicate by default which is why you need to define operator< for the type.

1
2
3
4
5
6
bool operator<(const point_t& rhs )
{
   return (x == rhs.x)
         ? (y <  rhs.y)
         : (x <  rhs.x);
}

Oct 27, 2009 at 7:23pm
By the way, there is a container called std::list. I'd be careful about naming members "list" in order to avoid confusing documentation. Naming a vector object "list" can result in confusing documentation since list has different types of member functions.
Topic archived. No new replies allowed.