?
Solved

STL sorting

Posted on 1998-02-08
1
Medium Priority
?
295 Views
Last Modified: 2013-12-14
I am using the Standard Template Library in a program that involves sorting linked lists.  I have an object defined that contains two variables, int ID and char TYPE.  I also have defined the operator< and operator == which involves the ID variable.  So an STL sort would sort the dequeue by the ID number.  I also want to sort by the TYPE.  For example, If I am sorting by ID number, and two identical number come up, I want to be able to break the tie by choosing the object with the highest TYPE priority.  I would also like to be able to sort by TYPE.  Is there any way to use the STL to sort a dequeue of object by more than one object variable?  
            Thank you
            Chris Blue
0
Comment
Question by:nexguy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 5

Accepted Solution

by:
yonat earned 200 total points
ID: 1181541
The standard allows you to pass a comparator to list::sort() as a parameter. However, most compilers do not yet support template member functions, and therefore they do not support this function.

The global sort, however, does not require this feature. It works on a deque, vector and built-in arrays. Here is an example:

#include <vector>
#include <algorithm>

struct MyStruct {
    int i;
    char c;
};

bool Compare_I(const MyStruct s1, const MyStruct s2)
{return (s1.i > s2.i);}

bool Compare_C(const MyStruct s1, const MyStruct s2)
{return (s1.c > s2.c);}

bool Compare_Both(const MyStruct s1, const MyStruct s2)
{
    if (s1.i == s2.i) return (s1.c > s2.c);
    else return (s1.i > s2.i);
}

int main ()
{
    using std::vector;
    using std::sort;

    vector<MyStruct> v;

    // put stuff into v

    sort(v.begin(), v.end(), Compare_I); // sort by first field
    sort(v.begin(), v.end(), Compare_C); // sort by second field
    sort(v.begin(), v.end(), Compare_Both); // sort by both

    return 0;
}


0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question