Hi everyone,

I'm dealing with a linked list that is supposed to be sorted. It's consisted of a series of strings which each of these strings are associated with 4 more strings. So, I'm looking for a way to sort this linked list with optimum way possible. I have some ideas myself:

1- Make 5 big array and copy everything into it.
Then sort the first string and make any change that is done to first to the rest of arrays.

2- Do the same algorithm using vectors.

3- Or do the same thing using linked list.

Let's say it's something like this, please bare in mind the numbers are in string format:

3 Tom Hanks 52 male actor
1 Sean Penn 50 male actor
2 Clive Owen 44 male actor

after sorting

1 Sean Penn 50 male actor
2 Clive Owen 44 male actor
3 Tom Hanks 52 male actor

Please let me know what could be the easiest and best way to do it.

Recommended Answers

All 5 Replies

You can use std::sort with a STL container, like a vector or a list.

sort() uses random access iterators, so it will not work on a list. STL lists use bidirectional iterators, but they also support their own sort method.

No other answer???

One way springs to mind, might not be as effective as a linked list, but still...

First, create a struct of a data you want e.g. struct Actor, containing the info given to you. Then push these Actors in to a vector, deque, table, whatever you want, sorted by the first number ( string2int ).

You can implement a sort strategy on your linked list, like bubble sort, quick sort, merge sort. Some strategies do not require an additionnal container for the answer.

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.