Dear All,

I'm trying to sort a list of structs using STL library.

struct myStruct {
  int i;
  string s;
};

bool compare(int first, int second)
{
  if (first < second)
    return true;
  else
    return false;
}

int main()
{
  list<myStruct> myList;
  
  /* Pretend that myList has elements with values */
  myList.sort(compare); // How could this be done!!! 

  /* I need to sort based on int i */
}

Recommended Answers

All 5 Replies

Well, you can use the std::sort algorithm in "#include <algorithm>". The only little problem is that it cannot be used with a list because list provides only bidirectional iterators, not random access iterators, which is required for the sort algorithm.

So you need to use a vector instead. Also, the compare function has to taken parameters of your struct type. So simply put:

#include <vector>
#include <algorithm>
using namespace std;

struct myStruct {
  int i;
  string s;
};

bool compare(const myStruct& first, const myStruct& second)
{
  if (first.i < second.i)
    return true;
  else
    return false;
}

int main()
{
  vector<myStruct> myList;
  
  /* Pretend that myList has elements with values */
  sort(myList.begin(),myList.end(),compare); //that's it! 

}

Well, you can use the std::sort algorithm in "#include <algorithm>". The only little problem is that it cannot be used with a list because list provides only bidirectional iterators, not random access iterators, which is required for the sort algorithm.

The standard list class has its own sort member that uses a different algorithm from the stand-alone sort algorithm. So if myList is a list, then instead of writing

sort(myList.begin(), myList.end(), compare);

you can write

myList.sort(compare);

@OP: your compare arguments has to be of type myStruct. Here is an example :

#include <iostream>
#include <list>
#include <string>
using namespace std;

struct Foo{
	string name;
	int age;
	Foo(){}
	Foo(const string& n, int a)
		: name(n), age(a){}
	void print()const{
		cout << "("<<name << "," << age << ")\n";
	}
};

bool compareNameFirst(const Foo& lhs, const Foo& rhs){
	if(lhs.name != rhs.name) return lhs.name < rhs.name;
	else return lhs.age < rhs.age;
}
int main(){       
	list<Foo> student;
	student.push_back(Foo("Alex",19));
	student.push_back(Foo("Maria",20));
	student.push_back(Foo("muhamed",20));
	student.push_back(Foo("Jeniffer",20));
	student.push_back(Foo("Alex",20));
	student.push_back(Foo("Maria",21));

	student.sort(compareNameFirst);
	
	list<Foo>::const_iterator itr = student.begin();
	while(itr != student.end()){
		itr->print();
		++itr;
	}
  return 0;
}

@firstPerson,

Thank you, that solved the problem. When I call my BOOL COMPARE method, I had to call it using my own structure type. so, this would be right:

bool compare(int first, int second)// Change to (myStruct first, myStruct second)
{
  if (first.i < second.i)
    return true;
  else
    return false;
}

Mike_2000_17 was right too. Thank you guys!!!

Also make sure its const-corrected. Instead of myStruct, make it const myStruct&

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.