Results 1 to 8 of 8

Thread: sorting two QStringLists simultaneously

  1. #1
    Join Date
    Sep 2010
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default sorting two QStringLists simultaneously

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: sorting two QStringLists simultaneously

    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

  3. #3
    Join Date
    Sep 2010
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: sorting two QStringLists simultaneously

    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>

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: sorting two QStringLists simultaneously

    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.
    Last edited by tbscope; 6th September 2010 at 06:37.

  5. #5
    Join Date
    Sep 2010
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: sorting two QStringLists simultaneously

    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?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: sorting two QStringLists simultaneously

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: sorting two QStringLists simultaneously

    and are you sure, your teacher won't read this thread here?

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: sorting two QStringLists simultaneously

    Quote Originally Posted by Lykurg View Post
    and are you sure, your teacher won't read this thread here?
    It's ok to give hints. It's forbidden to give solutions
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Sorting
    By bismitapadhy in forum Qt Programming
    Replies: 26
    Last Post: 30th January 2010, 16:59
  2. Zooming two plots simultaneously
    By Ban-chan in forum Qwt
    Replies: 2
    Last Post: 2nd September 2009, 19:42
  3. QListView sorting
    By fear in forum Qt Programming
    Replies: 14
    Last Post: 21st August 2007, 13:08
  4. Sorting a QVector
    By jiveaxe in forum Qt Programming
    Replies: 21
    Last Post: 11th August 2007, 19:24
  5. more network connections simultaneously
    By cmeliak in forum Qt Programming
    Replies: 9
    Last Post: 8th June 2006, 00:17

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.