Sorting Multi Levels

Are there any references for the algorithm to sort on more that 1 level.

I have a file with Date Sector Store

and I want to sort it Date and within Date by Sector and within Sector by Store
It's all in the element "compare" function. The compare function is what enforces the ordering you want, not the sort.
Use a stable sorting algorithm, first by store, then by sector and last by date.
Or provide a compare function that take into account those three parameters, with its priority.
Not sure what you mean but I used this for a 1 level sort

1
2
3
4
5
6
7
8
	bool Store::operator< (Store rhs)
	{
		return (date < rhs.date);

	}


Consider:

1
2
3
4
5
6
7
8
9
10
11
struct person_t {
    std::string lastName;
    std::string firstName;
};

bool operator<( const person_t& lhs, const person_t& rhs )
{
    if( lhs.lastName < rhs.lastName ) return true;
    if( rhs.lastName < lhs.lastName ) return false;
    return lhs.firstName < rhs.firstName;
}



Thanks!
I am assuming that I can have multiple overloaded operator<

So I can have different sorts based on the prototype.
bool Class::operator<(const Class &) const; Different prototypes?
You can have several compare functions (functions, static methods, function objects) and choose the one that you want.
Got it. Thanks!
Topic archived. No new replies allowed.