Results 1 to 3 of 3

Thread: Sorting a list containing QDates

  1. #1
    Join Date
    Jan 2012
    Posts
    83
    Qt products
    Qt4
    Platforms
    Windows

    Default Sorting a list containing QDates

    Hello!

    I want to sort a list containing QDates. After trying two ways, I could not overcome the probem. Could anyone help?
    Here are my codes:

    First, I used the function: qSort() as folowed:

    Qt Code:
    1. qSort(list.benin(), list.end())
    To copy to clipboard, switch view to plain text mode 

    Form this code, the output is a list in which only day names are sorted. For example, if I have Mo (for monday), Fr (for friday) and Sat (for saturday), the output order is: Fr 22 2014, Mo 12 2014 and Sat 25 2014.

    Second, with the following code, nothing is solved, the order for the imput is the same for the output.

    Qt Code:
    1. for(int g = 0; g<list.size()-1; g++)
    2. {
    3. Date1 = list[g];
    4.  
    5. for(int l = g+1; l<list.size(); l++)
    6. {
    7. Date2 = list[l];
    8.  
    9. days = date1.daysTo(date2);
    10.  
    11.  
    12. if(days < 0)
    13. {
    14.  
    15. temp = date1;
    16. date1 = date2;
    17. date2 = temp;
    18. }
    19.  
    20.  
    21. Date2.clear();
    22.  
    23. }
    24.  
    25. Date1.clear();
    26. }
    To copy to clipboard, switch view to plain text mode 

    I would appreciate any help.

    Many thanks in advance!

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Sorting a list containing QDates

    That sounds awfully like your list is a list of strings that represent dates and not QDates at all. QDate sorts correctly.
    Qt Code:
    1. #include <QtCore>
    2.  
    3. int main(int argc, char **argv) {
    4. QCoreApplication app(argc, argv);
    5.  
    6. QList<QDate> dates;
    7. dates << QDate(2014, 5, 2) // Friday
    8. << QDate(2014, 4, 28) // Monday
    9. << QDate(2014, 4, 26); // Saturday
    10.  
    11. qDebug() << "Before" << dates;
    12. qSort(dates.begin(), dates.end());
    13. qDebug() << "After " << dates;
    14.  
    15. return 0;
    16. }
    17.  
    18. // Before (QDate("Fri May 2 2014") , QDate("Mon Apr 28 2014") , QDate("Sat Apr 26 2014") )
    19. // After (QDate("Sat Apr 26 2014") , QDate("Mon Apr 28 2014") , QDate("Fri May 2 2014") )
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2012
    Posts
    83
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Sorting a list containing QDates

    Thanks ChrisW67 for your reply.

    It would be really good if my dates were in the format from your code, but my problem is that my dates are represented in the format:
    Sa Feb 22 2014:
    Assuming that I have a list of strings, I have converted these to dates as followed:

    Qt Code:
    1. dateConverted = QDate::fromString(Date, "dd MMM dd yyyy");
    To copy to clipboard, switch view to plain text mode 
    .

    Is there any way to tranform the format of my dates to yours?

    Many thanks in advance!.


    Added after 1 1:


    Hi, it was a bit stupid from me.

    The problem is solved. I was always using QString in my list and not QDate.

    Many thanks ChrisW67 for your help.

    The problem is solved.
    Last edited by Stanfillirenfro; 1st May 2014 at 10:36.

Similar Threads

  1. Replies: 4
    Last Post: 14th June 2012, 23:33
  2. Replies: 1
    Last Post: 23rd April 2011, 17:33
  3. sorting static QList<MyClass> list with qSort
    By oruccim in forum Qt Programming
    Replies: 3
    Last Post: 15th December 2010, 16:13
  4. List View with sections for alphabetialy sorted list
    By woodtluk in forum Qt Programming
    Replies: 4
    Last Post: 12th October 2010, 11:50
  5. Comparer of two QDates
    By TorAn in forum Qt Programming
    Replies: 1
    Last Post: 22nd April 2010, 20:56

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.