Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Sorting predefined types using STL sort in Open C++

From Wiki
Jump to: navigation, search

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

The article is believed to be still valid for the original topic scope.

Article Metadata
Tested with
Devices(s): Nokia 6220 Classic
Platform(s): S60 3rd Edition, FP2
Keywords: vector, list, sort(), list.sort()
Created: aknyman (03 Sep 2008)
Last edited: lpvalente (23 Sep 2014)



This code snippet shows how to use the C++ Standard Template Library (STL) sort function with predefined data types. The sort function needs at least two parameters: start and end. These iterator parameters are used to sort the range of elements between them. An optional third parameter has the default less-than operator as a value to compare elements.

Some STL containers cannot use regular the sort function and they provide specialized versions of sort as a member function. The list container is one of them and it also has an optional default value to compare elements.

Note: In order to use this code, you need to install the Open C/C++ plug-in.

This snippet can be self-signed.

MMP file

The following libraries are required:

LIBRARY libstdcpp.lib
LIBRARY libc.lib
LIBRARY euser.lib

Source file

#include <iostream>
#include <algorithm>
#include <list>
#include <vector>
#include <string>
using namespace std;
bool exampleCompare(string& s1, string& s2)
// compare string lengths
int len1 = s1.length();
int len2 = s2.length();
return len1 < len2;
int main()
//-- Sorting predefined data types --
// 1) int array
int intArray[] = { 3, 1, 2 };
int arrayElements = sizeof(intArray) / sizeof(intArray[0]);
sort(intArray, intArray + arrayElements);
for (int i=0; i<arrayElements; ++i)
cout << intArray[i] << ' '; //1 2 3
// 2) int vector
vector<int> intVector;
int vectorElements = intVector.size();
sort(intVector.begin(), intVector.end());
for (int i=0; i<vectorElements; ++i)
cout << intVector[i] << ' '; //1 2 3
// 3) string list
list<string> stringList;
list<string>::iterator stringIterator;
stringList.push_back ("second");
stringList.push_back ("first");
stringList.push_back ("third");
for (stringIterator = stringList.begin();
stringIterator != stringList.end();
cout << *stringIterator << ' '; //first second third
//use function exampleCompare to sort list again
for (stringIterator = stringList.begin();
stringIterator != stringList.end();
cout << *stringIterator << ' '; //first third second
return 0;


The STL sort function is used to sort arrays, vectors, and lists and the sorted values are displayed on the screen.

See also

Sorting class and struct types using STL sort in Open C++

This page was last modified on 23 September 2014, at 19:48.
123 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.