PDA

View Full Version : sorting two QStringLists simultaneously



hokie2012
6th September 2010, 07:15
ok, so I have two QStringLists ,one of dates, and one of numbers.. but each number matches up to a specific date...

i.e

06/10/10 2
04/10/10 1

but i want to sort it so the list is in chronological order but the numbers remain with their respective dates

i.e.

04/10/10 1
06/10/10 2

right now, i am breaking the dates apart from their numbers and sorting the dates just using the .sort function.. but i cannot figure out how to sort the dates and maintain their respective numbers..

TIA

tbscope
6th September 2010, 07:24
Why do you use 2 stringlists and not a map?

You can use the key (which would be the date) and put them in a stringlist, sort it and then use the value for each key (in the sorted list).
This way you only sort one list and the data is kept together in the map

hokie2012
6th September 2010, 07:28
I am only allowed to use these libraries

#include <QtCore/QCoreApplication>
#include <QtCore/QDate>
#include <QtCore/QFile>
#include <QtCore/QString>
#include <QtCore/QStringList>
#include <QtCore/QTextStream>

tbscope
6th September 2010, 07:32
Why? Is this homework?

You can use a file, which would be great too.
If you have an xml file, just load the dates in a string list, sort it and read the numbers associated with a date from the file.

Edit: or put the date and number together in one string, sort it, and separate them again.

hokie2012
6th September 2010, 07:41
yes its a hw assignment, ive completed 99% of it, just cant get this chrono sorting thing.. how would i put the date and number in one string, sort it, and then separate them again?

wysota
6th September 2010, 07:53
If you want to have both parts of the string as a single string then use qSort with a variant taking a lessThan() function and only sort according to the dates. But you don't have to do that - you can sort the date strings in such a way (using qSort again) so that you get a list of positions (indexes) of original dates in the sorted list. Then you can build the list of ints according to the calculated positions. A third possibility is to have a structure containing both the string and the int and sort that according to the key (string). As you can use QStringList you can also use QList. That would be the most academic approach to the problem.

Lykurg
6th September 2010, 08:42
and are you sure, your teacher won't read this thread here? :cool:

wysota
6th September 2010, 08:45
and are you sure, your teacher won't read this thread here? :cool:

It's ok to give hints. It's forbidden to give solutions :)