Click here to Skip to main content
15,887,371 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Sorting items, specifying multiple parameters Pin
Gunn31723-Feb-06 10:15
Gunn31723-Feb-06 10:15 
GeneralRe: Sorting items, specifying multiple parameters Pin
David Crow23-Feb-06 10:30
David Crow23-Feb-06 10:30 
AnswerRe: Sorting items, specifying multiple parameters Pin
Chris Losinger23-Feb-06 9:38
professionalChris Losinger23-Feb-06 9:38 
GeneralRe: Sorting items, specifying multiple parameters Pin
Gunn31723-Feb-06 10:11
Gunn31723-Feb-06 10:11 
GeneralRe: Sorting items, specifying multiple parameters Pin
Chris Losinger23-Feb-06 10:28
professionalChris Losinger23-Feb-06 10:28 
GeneralRe: Sorting items, specifying multiple parameters Pin
Gunn31723-Feb-06 11:08
Gunn31723-Feb-06 11:08 
GeneralRe: Sorting items, specifying multiple parameters Pin
Chris Losinger23-Feb-06 11:17
professionalChris Losinger23-Feb-06 11:17 
AnswerRe: Sorting items, specifying multiple parameters Pin
Stephen Hewitt23-Feb-06 11:20
Stephen Hewitt23-Feb-06 11:20 
Using qsort is old fashioned, you should be using STL - it's faster safer and more flexible. Here's how I'd do it:
----------

#include <iostream>
#include <algorithm>
#include <iterator>
using namespace std;

// Our data.
struct Blah
{
Blah(int Type, const char* pName) :
m_Type(Type), m_pName(pName)
{
}

int m_Type;
const char* m_pName;
};

// Print out a blah.
ostream& operator<<(ostream& s, const Blah& b)
{
s << "Type: " << b.m_Type << ", Name: \"" << b.m_pName << "\"";
return s;
}

// Sort functors.
struct SortByType
{
bool operator()(const Blah& l, const Blah& r) const
{
return l.m_Type < r.m_Type;
}
};

struct SortByName
{
bool operator()(const Blah& l, const Blah& r) const
{
return strcmp(l.m_pName, r.m_pName) < 0;
}
};

struct SortByTypeThenName
{
bool operator()(const Blah& l, const Blah& r) const
{
return SortByType()(l, r) | (!SortByType()(r, l) && SortByName()(l, r));
}
};

// The data.
Blah g_Blahs[] =
{
Blah(1, "George"), Blah(2, "Liam"), Blah(2, "Hank"),
Blah(0, "Abigail"), Blah(1, "Bob"), Blah(0, "Jessica"),
Blah(2, "Con"), Blah(1, "Kyle"), Blah(0, "Faye")
};

int main(int argc, char* argv[])
{
Blah* pBegin = &g_Blahs[0];
Blah* pEnd = &g_Blahs[sizeof(g_Blahs)/sizeof(g_Blahs[0])];
ostream_iterator<Blah> oi(cout, "\n");

// Sort by type.
cout << "Type:\n";
sort(pBegin, pEnd, SortByType());
copy(pBegin, pEnd, oi);
cout << "\n";

// Sort by name.
cout << "Name:\n";
sort(pBegin, pEnd, SortByName());
copy(pBegin, pEnd, oi);
cout << "\n";

// Sort by type and name.
cout << "Type and name:\n";
sort(pBegin, pEnd, SortByTypeThenName());
copy(pBegin, pEnd, oi);
cout << "\n";

return 0;
}


Steve

-- modified at 17:29 Thursday 23rd February, 2006
FIXED error in SortByTypeThenName.

GeneralRe: Sorting items, specifying multiple parameters Pin
Gunn31728-Feb-06 2:40
Gunn31728-Feb-06 2:40 
QuestionVisual Studio 6.0 with other compliers Pin
softwaremonkey23-Feb-06 8:21
softwaremonkey23-Feb-06 8:21 
AnswerRe: Visual Studio 6.0 with other compliers Pin
basementman23-Feb-06 8:34
basementman23-Feb-06 8:34 
GeneralRe: Visual Studio 6.0 with other compliers Pin
softwaremonkey23-Feb-06 8:36
softwaremonkey23-Feb-06 8:36 
Questiondevice notification Pin
Manu_8123-Feb-06 7:19
Manu_8123-Feb-06 7:19 
AnswerRe: device notification Pin
David Crow23-Feb-06 8:22
David Crow23-Feb-06 8:22 
QuestionVoice chat using VC++ Pin
gaurav_dixit23-Feb-06 7:05
gaurav_dixit23-Feb-06 7:05 
AnswerRe: Voice chat using VC++ Pin
J512198223-Feb-06 23:53
J512198223-Feb-06 23:53 
QuestionHow to know that I m connected to remote socket or not ? Pin
zinc_z23-Feb-06 6:47
zinc_z23-Feb-06 6:47 
Questionpreview file/directory deletion Pin
Chintoo72323-Feb-06 6:31
Chintoo72323-Feb-06 6:31 
AnswerRe: preview file/directory deletion Pin
peterchen23-Feb-06 6:39
peterchen23-Feb-06 6:39 
QuestionRepaint Problem Pin
Deviantizh23-Feb-06 6:17
Deviantizh23-Feb-06 6:17 
AnswerRe: Repaint Problem Pin
peterchen23-Feb-06 6:30
peterchen23-Feb-06 6:30 
QuestionRe: Repaint Problem Pin
Deviantizh23-Feb-06 7:05
Deviantizh23-Feb-06 7:05 
AnswerRe: Repaint Problem Pin
peterchen23-Feb-06 10:35
peterchen23-Feb-06 10:35 
QuestionCode-Generation options Pin
Themis23-Feb-06 5:37
Themis23-Feb-06 5:37 
AnswerRe: Code-Generation options Pin
peterchen23-Feb-06 6:36
peterchen23-Feb-06 6:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.