Click here to Skip to main content
15,892,005 members
Articles / Desktop Programming / MFC

Using the std::sort() Method

Rate me:
Please Sign up or sign in to vote.
4.52/5 (11 votes)
27 Apr 2000 221.3K   767   20  
An introduction to sorting using STL
// STLSort.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <vector>
#include <algorithm>
#include <iostream>

typedef double DATE;

// Base class we're using to provide a function with a sortable value
class CHistoryItem
{
public:
	CHistoryItem(DATE rTimestamp)
	{	
		m_Timestamp = rTimestamp; 
	}

	// Return the time stamp value
	DATE& GetTimestamp()
	{	
		return m_Timestamp;	
	}

	// Indicate the item type as a string
	virtual	char* ItemName() = 0;

	// Output the item to a stream
	void OutputToStream(std::ostream& os)
	{
		os << ItemName() << "\t" << m_Timestamp << "\n";
	}

protected:
	DATE	m_Timestamp;
};


// Repair entry
class CRepairItem : public CHistoryItem
{
public:
	CRepairItem(DATE rTimestamp)
		: CHistoryItem(rTimestamp)
	{
	}

	// Indicate that this is a repair
	char* ItemName()	{	return "Repair: ";	}
};

// Installtion entry
class CInstallItem : public CHistoryItem
{
public:
	CInstallItem(DATE rTimestamp)
		: CHistoryItem(rTimestamp)	
	{	
	}

	// Indicate that this is a installation
	char* ItemName()	{	return "Install: ";	}
};

// Purchase entry
class CPurchaseItem : public CHistoryItem
{
public:
	CPurchaseItem(DATE rTimestamp)
		: CHistoryItem(rTimestamp)
	{
	}

	// Indicate that this is a installation
	char* ItemName()	{	return "Purchase: ";	}
};

///////////////////////////////

// A vector based class to contain our history items
class CHistoryVector : public std::vector<CHistoryItem*>
{
public:
	// Make sure that the history items are de-allocated so we don't leak
	~CHistoryVector()
	{
		for (iterator pItem=begin(); pItem != end(); ++pItem)
			delete *pItem;
	}
};

// Ascending date sorting function
struct SAscendingDateSort
{	
	bool operator()(CHistoryItem*& rpStart, CHistoryItem*& rpEnd)
	{
		return rpStart->GetTimestamp() < rpEnd->GetTimestamp();
	}
};

// Descending date sorting function
struct SDescendingDateSort
{	
	bool operator()(CHistoryItem*& rpStart, CHistoryItem*& rpEnd)
	{
		return rpStart->GetTimestamp() > rpEnd->GetTimestamp();
	}
};

// Stuff a vector with data & then sort
int main(int argc, char* argv[])
{
	CHistoryVector	HistoryVector;

	// Put two repair items in the vector
	HistoryVector.push_back(new CRepairItem(2*365.0));
	HistoryVector.push_back(new CRepairItem(5*365.0));

	// Now stick three installations items in the vector
	HistoryVector.push_back(new CInstallItem(3*365.0));
	HistoryVector.push_back(new CInstallItem (6*365.0));
	HistoryVector.push_back(new CInstallItem (4*365.0));

	// Finally we need to add a purchase item to the vector
	HistoryVector.push_back(new CPurchaseItem (1*365.0));

	//==================================
	// Sort the items in ascending order
	std::sort(HistoryVector.begin(), HistoryVector.end(), SAscendingDateSort());

	// Now show the results!
	std::cout << "Ascending Sort\n";
	for (long lEle=0; lEle < HistoryVector.size(); ++lEle)
		HistoryVector[lEle]->OutputToStream(std::cout);

	//==================================
	// Sort the items in descending order
	std::sort(HistoryVector.begin(), HistoryVector.end(), SDescendingDateSort());

	// Now show the new results!
	std::cout << "\nDescending Sort\n";
	for (lEle=0; lEle < HistoryVector.size(); ++lEle)
		HistoryVector[lEle]->OutputToStream(std::cout);


	getchar();	// Pause for a response
	return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions