sorting std::lists using std::sort

c++, list, sorting, std

Solution

You can't use `std::sort` to sort `std::list`, because `std::sort` requires iterators to be random access, and `std::list` iterators are only bidirectional.

However, `std::list` has a member function `sort` that will sort it:

list.sort();
// if you want to use a comparator different from the default one:
// list.sort(comparator);

Problem

Possible Duplicate: Sort list using stl sort function My question is can we sort two std::lists using std::sort function? I have 2 string lists ``` std::list<std::string>list1, list2; .....//entering values to list std::sort(list1.begin(), list1.end()); std::sort(list2.begin(), list2.end()); ``` while i am sorting these lists i am getting error. I tried with std::vector, at this time the sort works. The error is like C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xutility(1158) : see declaration of 'std::operator -' 1>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\algorithm(3642): error C2784: '_Base1::difference_type std::operator - (const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'std::_List_iterator<_Mylist>' 1> with 1> [ 1> _Mylist=std::_List_valstd::string,std::allocator<std::string> 1> ] I have to know that only std::sort supports lists?

Original source

Related problems