Results 1 to 13 of 13

Thread: Bug in QStringList custom sort

  1. #1
    Join Date
    Feb 2013
    Posts
    71
    Platforms
    Windows
    Thanks
    6
    Thanked 3 Times in 3 Posts

    Default Bug in QStringList custom sort

    Qt Code:
    1. QStringList listNames;
    2. QStringList tempList;
    3. listNames << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
    4. list << "Jan" << "Mar" << "May" << "Jul" << "Sep" << "Nov" << "Feb" << "Apr" << "Jun" << "Aug" << "Oct" << "Dec" << "Jan" << "Mar" << "May" << "Jul" << "Sep" << "Nov" << "Feb" << "Apr" << "Jun" << "Aug" << "Oct" << "Dec" << "Jan" << "Mar" << "May" << "Jul" << "Sep" << "Nov" << "Feb" << "Apr" << "Jun" << "Aug" << "Oct" << "Dec";
    5. int startSize = list.size();
    6. int num;
    7. for(int i = 0; i<startSize; i++){
    8. for(int j = 0; j<listNames.size(); j++){
    9. if(list.contains(listNames[j],Qt::CaseInsensitive)){
    10. num = list.indexOf(listNames[j]);
    11. //cout << num << endl;
    12. }
    13. }
    14. tempList << list[num];
    15. list.removeAt(num);
    16. }
    17. for(int i = 0; i<startSize; i++){
    18. list << tempList[tempList.size()-1];
    19. tempList.removeLast();
    20. }
    21.  
    22.  
    23. for(int i = 0; i<list.size(); i++){
    24. cout << list[i].toStdString() << endl;
    25. }
    To copy to clipboard, switch view to plain text mode 
    in this example the user input would be the QStringList list and the order they are being sorted is QStringList listNames
    it works perctly if the user inputs the same exact as the sorting order.

    but what I would like to do is if the user inputs "jan" it will know it is the same as "Jan" I can check to see if it is the same by using contains which will yeah find it...but
    when I do the index of it I will always get -1 because I can not think of a way to do
    Qt Code:
    1. indexOf(QString, Qt::CaseInsensitive)
    To copy to clipboard, switch view to plain text mode 
    is this even possible?

    EDIT: well I can think of one way but I would prefer not to do it.
    it would be to use QString::toUpper or QString::toLower for both of the QStringLists and it wouldnt matter what they input.
    but the problem with that is.....I want the end result to be first letter Uppercase rest of the letters Lowercase

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Bug in QStringList custom sort

    So is your question about sorting or searching?
    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.


  3. #3
    Join Date
    Feb 2013
    Posts
    71
    Platforms
    Windows
    Thanks
    6
    Thanked 3 Times in 3 Posts

    Default Re: Bug in QStringList custom sort

    kinda both I made it so I can sort and it works fine if A = A or a = a and not if A = a or a = A which is what I am trying to do
    I figured how to make all the them A = A no matter what they input by doing
    Qt Code:
    1. list[i] = list[i].toLower(); list[i][0] = list[i][0].toTitleCase();
    To copy to clipboard, switch view to plain text mode 
    but then the app crashes and never runs and I narrowed it down to this line.
    Qt Code:
    1. list.removeAt(num)
    To copy to clipboard, switch view to plain text mode 
    I didnt change anything besides in the list I made a few things upper and a few lower case but then when I couted i got the same exact for the list and listNames but doesnt work and If i change the list back to normal and comment out the list = list things it works fine but that shouldnt even effect it because I am setting the old one to a new one that is the same excact as the old. any suggestions?

    this WORKS but only if Jan = Jan and not jan = Jan or jAn = Jan
    Qt Code:
    1. QStringList listNames;
    2. QStringList tempList;
    3. //listNames << "January" << "February" << "March" << "April" << "May" << "June" << "July" << "August" << "September" << "October" << "November" << "December";
    4. listNames << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
    5. list << "Jan" << "Mar" << "May" << "Jul" << "Sep" << "Nov" << "Feb" << "Apr" << "Jun" << "Aug" << "Oct" << "Dec" << "Jan" << "Mar" << "May" << "Jul" << "Sep" << "Nov" << "Feb" << "Apr" << "Jun" << "Aug" << "Oct" << "Dec" << "Jan" << "Mar" << "May" << "Jul" << "Sep" << "Nov" << "Feb" << "Apr" << "Jun" << "Aug" << "Oct" << "Dec";
    6. int startSize = list.size();
    7. int num;
    8.  
    9. for(int i = 0; i<startSize; i++){
    10. //list[i] = list[i].toLower();
    11. //list[i][0] = list[i][0].toTitleCase();
    12. //cout << list[i].toStdString() << endl;
    13. for(int j = 0; j<listNames.size(); j++){
    14. if(list.contains(listNames[j],Qt::CaseInsensitive)){
    15. num = list.indexOf(listNames[j]);
    16. //cout << num << endl;
    17. //cout << "LIST AT NUM: " << list.at(num).toStdString() << endl;
    18.  
    19. }
    20. }
    21. //cout << list[i].toStdString() << flush;
    22. //cout << tempList[i].toStdString() << flush;
    23. tempList << list[num];
    24. list.removeAt(num);
    25. }
    26. for(int i = 0; i<startSize; i++){
    27. list << tempList[tempList.size()-1];
    28. tempList.removeLast();
    29. }
    30.  
    31.  
    32. for(int i = 0; i<list.size(); i++){
    33. cout << list[i].toStdString() << endl;
    34. }
    To copy to clipboard, switch view to plain text mode 

    and this does not work and I have no idea why(well it is something to do with the list[i][0] = list[i][0] and the removeAt(num) lines):
    Qt Code:
    1. QStringList listNames;
    2. QStringList tempList;
    3. //listNames << "January" << "February" << "March" << "April" << "May" << "June" << "July" << "August" << "September" << "October" << "November" << "December";
    4. listNames << "JaN" << "FeB" << "mAr" << "Apr" << "May" << "Jun" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
    5. list << "Jan" << "Mar" << "May" << "Jul" << "Sep" << "Nov" << "Feb" << "Apr" << "Jun" << "Aug" << "Oct" << "Dec" << "Jan" << "Mar" << "May" << "Jul" << "Sep" << "Nov" << "Feb" << "Apr" << "Jun" << "Aug" << "Oct" << "Dec" << "Jan" << "Mar" << "May" << "Jul" << "Sep" << "Nov" << "Feb" << "Apr" << "Jun" << "Aug" << "Oct" << "Dec";
    6. int startSize = list.size();
    7. int num;
    8.  
    9. for(int i = 0; i<startSize; i++){
    10. list[i] = list[i].toLower();
    11. list[i][0] = list[i][0].toTitleCase();
    12. //cout << list[i].toStdString() << endl;
    13. for(int j = 0; j<listNames.size(); j++){
    14. if(list.contains(listNames[j],Qt::CaseInsensitive)){
    15. num = list.indexOf(listNames[j]);
    16. //cout << num << endl;
    17. //cout << "LIST AT NUM: " << list.at(num).toStdString() << endl;
    18.  
    19. }
    20. }
    21. //cout << list[i].toStdString() << flush;
    22. //cout << tempList[i].toStdString() << flush;
    23. tempList << list[num];
    24. list.removeAt(num);
    25. }
    26. for(int i = 0; i<startSize; i++){
    27. list << tempList[tempList.size()-1];
    28. tempList.removeLast();
    29. }
    30.  
    31.  
    32. for(int i = 0; i<list.size(); i++){
    33. cout << list[i].toStdString() << endl;
    34. }
    To copy to clipboard, switch view to plain text mode 


    Added after 6 minutes:


    okay when I closed qt and reopened it, it worked I have no idea why but it works for me now
    thanks anyways though wysota
    Last edited by giblit; 29th March 2013 at 02:09.

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

    Default Re: Bug in QStringList custom sort

    Your code is extremely inefficient. It's complexity is O(n^4) (n*n*n*n where each 'n' comes from a for loop, a contains call or a indexOf call) while it can be much faster and simpler. Optimally sorting a list is O(n * lgn) and searching in such a sorted list is O(lgn) which can be both achieved using qSort (or std::sort) and qLowerBound() and passing a custom comparator that will ignore the case of compared strings. Alternatively you can just use a map where the key is lower case string and the value is a list of proper case (or title case or whatever you want) strings. Then it's just a matter of iterating through the map (which is sorted by key) or asking it to retrieve values for the key you want.
    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.


  5. #5
    Join Date
    Feb 2013
    Posts
    71
    Platforms
    Windows
    Thanks
    6
    Thanked 3 Times in 3 Posts

    Default Re: Bug in QStringList custom sort

    how would I sort by using a QMap I couldnt figure out how to use Qmaps even after reading the class references
    I can figure out this
    Qt Code:
    1. QMap<QString, int> map;
    2. map["January"] = 1;
    3. map["December"] = 12;
    To copy to clipboard, switch view to plain text mode 
    but after that I am a bit confused on how I would sort that and that would only be for the inital values not for the inputed values which is what I am trying to sort.

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

    Default Re: Bug in QStringList custom sort

    You don't have to sort it, it's already sorted by key name. Just use whatever you want to sort by for keys and real data for values.
    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
    Feb 2013
    Posts
    71
    Platforms
    Windows
    Thanks
    6
    Thanked 3 Times in 3 Posts

    Default Re: Bug in QStringList custom sort

    okay cool thanks i understand how to use the QMap now, the first value is the key name and the second item is the key value so I just just use this:
    Qt Code:
    1. QStringList listnames;
    2. listNames << "January" << "Jan" << "1" << "Feb" << "Mar" << "Apr" << "May" << "Jun" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
    3. //user inputs something list << "INPUT";
    4. for(int i = 0; i<list.size(); i++){
    5. list[i] = list[i].toLower();
    6. list[i][0] = list[i][0].toTitleCase();
    7. }
    8. QMap<QString,int> map;
    9. for(int i = 0; i<listNames.size(); i++){
    10. map[listNames[i]] = i;
    11. }
    To copy to clipboard, switch view to plain text mode 

    thanks for the help wysota =]

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

    Default Re: Bug in QStringList custom sort

    That's still superflous, you don't need this first for loop. And I have no idea why you're putting integers as values for the map. If you do that, you get exactly the same output as if you were using a list (because the index of the list is equal to the value you put into the map). I suggest you first try to determine what you want to achieve and how you want to achieve it, then understand why it works the way it works (or not) and only then start implementing your solution. Programming is not rocket science but you can't blindly manipulate code hoping something that works for you comes out of it.
    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.


  9. #9
    Join Date
    Feb 2013
    Posts
    71
    Platforms
    Windows
    Thanks
    6
    Thanked 3 Times in 3 Posts

    Default Re: Bug in QStringList custom sort

    I have two lists, the first is the correct order which is why I am setting them to that position in the QMap, and the second list is the user input list which could be in any order which I am setting to the same order as the first list which is also why I have the first loop that is making sure they are all in the same format (first letter capital second letter small)
    and it works for me I even couted it =p
    maybe you are not understanding what I did.
    here is the full code that seems to work
    Qt Code:
    1. QStringList listNames;
    2. listNames << "January" << "Jan" << "01" << "1" << "February" << "Feb" << "02" << "2" << "March" << "Mar" << "03" << "3" << "April" << "Apr" << "04" << "4" << "May" << "05" << "5" << "June" << "Jun" << "06" << "6" << "July" << "Jul" << "07" << "7" << "August " << "Aug" << "08" << "8" << "September" << "Sep" << "09" << "9" << "October" << "Oct" << "10" << "Novemeber" << "Nov" << "11" << "Decemeber" << "Dec" << "12";
    3. list << "JaN" << "MaR" << "May" << "Jul" << "Sep" << "Nov" << "Feb" << "Apr" << "Jun" << "Aug" << "oct" << "DEC" << "Jan" << "Mar" << "May" << "Jul" << "Sep" << "Nov" << "Feb" << "Apr" << "Jun" << "Aug" << "Oct" << "Dec" << "Jan" << "Mar" << "May" << "Jul" << "Sep" << "Nov" << "Feb" << "Apr" << "Jun" << "Aug" << "Oct" << "Dec" << "January" << "1";
    4. for(int i = 0; i<list.size(); i++){
    5. list[i] = list[i].toLower();
    6. list[i][0] = list[i][0].toTitleCase();
    7. }
    8.  
    9. cout << "BEFORE SORT: " << flush;
    10. for(int i = 0; i<list.size();i++){
    11. cout << list[i].toStdString() << flush;
    12. }
    13.  
    14. QMap<QString,int> map;
    15. for(int i = 0; i<listNames.size(); i++){
    16. map[listNames[i]] = i;
    17. }
    18. QList<int> list2;
    19. for(int i = 0; i<list.size(); i++){
    20. //cout << list[i].toStdString() << ": " << flush;
    21. //list[i] = map[list[i]];
    22. //cout << map[list[i]] << endl;
    23. list2 << map[list[i]];
    24. qSort(list2.begin(),list2.end());
    25. }
    26. list.clear();
    27. for(int j = 0; j<list2.size();j++){
    28. //cout << list2[j] << endl;
    29. list << map.key(list2[j]);
    30. //cout << map.key(list2[j]).toStdString() << endl;
    31. //cout << map[list2[j]].toStdString() << endl;
    32. }
    33. cout << endl << "AFTER SORT: " << flush;
    34. for(int i = 0; i<list.size();i++){
    35. cout << list[i].toStdString() << flush;
    36. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by giblit; 30th March 2013 at 23:58. Reason: forgot a few lines

  10. #10
    Join Date
    Feb 2013
    Posts
    71
    Platforms
    Windows
    Thanks
    6
    Thanked 3 Times in 3 Posts

    Default Re: Bug in QStringList custom sort

    okay so I can sort by months, or days, or years...by using QMap with this code:
    Qt Code:
    1. QStringList sortedMonth, inMonth;
    2. sortedMonth << "January" << "February" << "March" << "April" << "May" << "June" << "July" << "August" << "September" << "October" << "November" << "December";
    3. inMonth << "May" << "Dec" << "Jan" << "Sep";
    4. for(int i = 0; i<inMonth.size(); i++){
    5. if(inMonth.contains("Jan",Qt::CaseInsensitive)){
    6. inMonth.replace(inMonth.indexOf("Jan"),"January");
    7. } else if(inMonth.contains("Feb",Qt::CaseInsensitive)){
    8. inMonth.replace(inMonth.indexOf("Feb"),"February");
    9. } else if(inMonth.contains("Mar", Qt::CaseInsensitive)){
    10. inMonth.replace(inMonth.indexOf("Mar"),"March");
    11. } else if(inMonth.contains("Apr",Qt::CaseInsensitive)){
    12. inMonth.replace(inMonth.indexOf("Apr"),"April");
    13. } else if(inMonth.contains("Jun",Qt::CaseInsensitive)){
    14. inMonth.replace(inMonth.indexOf("Jun"),"June");
    15. } else if(inMonth.contains("Jul",Qt::CaseInsensitive)){
    16. inMonth.replace(inMonth.indexOf("Jul"),"July");
    17. } else if(inMonth.contains("Aug",Qt::CaseInsensitive)){
    18. inMonth.replace(inMonth.indexOf("Aug"),"August");
    19. } else if(inMonth.contains("Sep",Qt::CaseInsensitive)){
    20. inMonth.replace(inMonth.indexOf("Sep"),"September");
    21. } else if(inMonth.contains("Oct",Qt::CaseInsensitive)){
    22. inMonth.replace(inMonth.indexOf("Oct"),"October");
    23. } else if(inMonth.contains("Nov",Qt::CaseInsensitive)){
    24. inMonth.replace(inMonth.indexOf("Nov"),"November");
    25. } else if(inMonth.contains("Dec",Qt::CaseInsensitive)){
    26. inMonth.replace(inMonth.indexOf("Dec"),"December");
    27. }
    28. }
    29. QMap<QString,int> map, map2, map3;
    30. for(int i = 0; i<sortedMonth.size();i++){
    31. map[sortedMonth[i]] = i;
    32. }
    33. QList<int> listorder, listorder2, listorder3;
    34. for(int i = 0; i<inMonth.size(); i++){
    35. listorder << map[inMonth[i]];
    36. }
    37. qSort(listorder.begin(),listorder.end());
    38. inMonth.clear();
    39. for(int i = 0; i<listorder.size();i++){
    40. inMonth << map.key(listorder[i]);
    41. }
    42. for(int i = 0; i<inMonth.size();i++){
    43. cout << inMonth[i].toStdString() << endl;
    44. }
    45. treeWidget = new QTreeWidget;
    46. for(int i = 0; i<inMonth.size(); i++){
    47. item[i] = new QTreeWidgetItem(treeWidget);
    48. item[i]->setText(0, inMonth[i]);
    49. treeWidget->insertTopLevelItem(i, item[i]);
    50. }
    51. treeWidget->setColumnCount(1);
    52. setCentralWidget(treeWidget);
    To copy to clipboard, switch view to plain text mode 

    my problem though is say I have the date January 1 and January 2 I want january 1 to come before january 2
    and the only way I can do it is if I have the same size sortedDays as the SortedMonths so that the indexes match when I map, but....if I do it dynamically which I want to..I wont always know the days and if I put in days 1-31 for the sorted then the index will not match and it will just put the numbers in order instead of for the coresponding date.

    is there a way I could sort a QStringList that contains these QStrings January 3, December 21, March 28, January 25, October 15, December 7, March 7, October 28. so that it would sort months first, e.g.(Jan, Mar, Oct, Dec) then after doing that it would be Jan 3, Jan 25, March 28, March 7, Oct 15, Oct 28, Dec 21, Dec 7 . then After the first sort of months it would sort again based on days and have an end product of: Jan 3, Jan 25, March 7, March 28, Oct 15, Oct 28, Dec 7, Dec 21

    sorry for taking so much time to do this and hogging up the forum =/


    Added after 14 minutes:


    Quote Originally Posted by giblit View Post
    okay so I can sort by months, or days, or years...by using QMap with this code:
    Qt Code:
    1. QStringList sortedMonth, inMonth;
    2. sortedMonth << "January" << "February" << "March" << "April" << "May" << "June" << "July" << "August" << "September" << "October" << "November" << "December";
    3. inMonth << "May" << "Dec" << "Jan" << "Sep";
    4. for(int i = 0; i<inMonth.size(); i++){
    5. if(inMonth.contains("Jan",Qt::CaseInsensitive)){
    6. inMonth.replace(inMonth.indexOf("Jan"),"January");
    7. } else if(inMonth.contains("Feb",Qt::CaseInsensitive)){
    8. inMonth.replace(inMonth.indexOf("Feb"),"February");
    9. } else if(inMonth.contains("Mar", Qt::CaseInsensitive)){
    10. inMonth.replace(inMonth.indexOf("Mar"),"March");
    11. } else if(inMonth.contains("Apr",Qt::CaseInsensitive)){
    12. inMonth.replace(inMonth.indexOf("Apr"),"April");
    13. } else if(inMonth.contains("Jun",Qt::CaseInsensitive)){
    14. inMonth.replace(inMonth.indexOf("Jun"),"June");
    15. } else if(inMonth.contains("Jul",Qt::CaseInsensitive)){
    16. inMonth.replace(inMonth.indexOf("Jul"),"July");
    17. } else if(inMonth.contains("Aug",Qt::CaseInsensitive)){
    18. inMonth.replace(inMonth.indexOf("Aug"),"August");
    19. } else if(inMonth.contains("Sep",Qt::CaseInsensitive)){
    20. inMonth.replace(inMonth.indexOf("Sep"),"September");
    21. } else if(inMonth.contains("Oct",Qt::CaseInsensitive)){
    22. inMonth.replace(inMonth.indexOf("Oct"),"October");
    23. } else if(inMonth.contains("Nov",Qt::CaseInsensitive)){
    24. inMonth.replace(inMonth.indexOf("Nov"),"November");
    25. } else if(inMonth.contains("Dec",Qt::CaseInsensitive)){
    26. inMonth.replace(inMonth.indexOf("Dec"),"December");
    27. }
    28. }
    29. QMap<QString,int> map, map2, map3;
    30. for(int i = 0; i<sortedMonth.size();i++){
    31. map[sortedMonth[i]] = i;
    32. }
    33. QList<int> listorder, listorder2, listorder3;
    34. for(int i = 0; i<inMonth.size(); i++){
    35. listorder << map[inMonth[i]];
    36. }
    37. qSort(listorder.begin(),listorder.end());
    38. inMonth.clear();
    39. for(int i = 0; i<listorder.size();i++){
    40. inMonth << map.key(listorder[i]);
    41. }
    42. for(int i = 0; i<inMonth.size();i++){
    43. cout << inMonth[i].toStdString() << endl;
    44. }
    45. treeWidget = new QTreeWidget;
    46. for(int i = 0; i<inMonth.size(); i++){
    47. item[i] = new QTreeWidgetItem(treeWidget);
    48. item[i]->setText(0, inMonth[i]);
    49. treeWidget->insertTopLevelItem(i, item[i]);
    50. }
    51. treeWidget->setColumnCount(1);
    52. setCentralWidget(treeWidget);
    To copy to clipboard, switch view to plain text mode 

    my problem though is say I have the date January 1 and January 2 I want january 1 to come before january 2
    and the only way I can do it is if I have the same size sortedDays as the SortedMonths so that the indexes match when I map, but....if I do it dynamically which I want to..I wont always know the days and if I put in days 1-31 for the sorted then the index will not match and it will just put the numbers in order instead of for the coresponding date.

    is there a way I could sort a QStringList that contains these QStrings January 3, December 21, March 28, January 25, October 15, December 7, March 7, October 28. so that it would sort months first, e.g.(Jan, Mar, Oct, Dec) then after doing that it would be Jan 3, Jan 25, March 28, March 7, Oct 15, Oct 28, Dec 21, Dec 7 . then After the first sort of months it would sort again based on days and have an end product of: Jan 3, Jan 25, March 7, March 28, Oct 15, Oct 28, Dec 7, Dec 21

    sorry for taking so much time to do this and hogging up the forum =/
    argh nevermind I don't know what the hell I was thinking....all I had to do was convert the dates into numbers eg Jan 18 2010 and december 31 2009 would be: 2010118 and 20091231 then all I have to do is sort the numbers from least to greatest (eg the farthest back to most current) sorry to waste everyones time.

    EDIT: got it working nicely =] thanks agian for the help earlier appreciate it.
    Qt Code:
    1. QStringList months, dates;
    2. QDate date[9];
    3. months << "January" << "February" << "March" <<"April" << "May" << "June" << "July" << "August" << "September" << "October" << "November" << "December";
    4. //format is MMddyyyy
    5. dates << "January 01, 2010"<< "December 31, 2010"<< "November 03 2009" << "April 25, 2010"<< "January 03, 2011"<< "January 02, 2011" << "March/25/2013" << "09-21-2008" << "October/03-2012";
    6. for(int i = 0; i<dates.size();i++){
    7. dates[i].remove(" ");
    8. dates[i].remove(",");
    9. dates[i].remove("/");
    10. dates[i].remove("-");
    11. for(int j = 0;j<12;j++){
    12. if(j<8){
    13. dates[i].replace(months[j],QString("0%1").arg(j+1));
    14. } else {
    15. dates[i].replace(months[j],QString("%1").arg(j+1));
    16. }
    17. }
    18. //cout << dates[i].toStdString() << endl;
    19. dates[i].prepend(dates[i].right(4));
    20. dates[i].remove(8,4);;
    21. //cout << dates[i].toStdString() << endl;
    22. }
    23. QList<int> dateNum;
    24. for(int i =0; i<dates.size();i++){
    25. dateNum << dates[i].toInt();
    26. }
    27. dates.clear();
    28. qSort(dateNum.begin(),dateNum.end());
    29. for(int i = 0; i<dateNum.size();i++){
    30. dates << QString("%1").arg(dateNum[i]);
    31. dates[i].append(dates[i].left(4));
    32. dates[i].remove(0,4);
    33. date[i] = date[i].fromString(dates[i],"MMddyyyy");
    34. }
    35. for(int i = 0; i<dates.size();i++){
    36. // cout << dateNum[i] << endl;
    37. //cout << dates[i].toStdString() << endl;
    38. cout << date[i].toString("MMMM dd, yyyy").toStdString() << endl;
    39. }
    To copy to clipboard, switch view to plain text mode 

    the output from that is:
    September 21, 2008
    November 03, 2009
    January 01, 2010
    April 25, 2010
    December 31, 2010
    January 02, 2011
    January 03, 2011
    October 03, 2012
    March 25, 2013
    Last edited by giblit; 31st March 2013 at 10:04.

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Bug in QStringList custom sort

    Maybe you should just sort dates and not strings?
    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.


  12. The following user says thank you to wysota for this useful post:

    giblit (31st March 2013)

  13. #12
    Join Date
    Feb 2013
    Posts
    71
    Platforms
    Windows
    Thanks
    6
    Thanked 3 Times in 3 Posts

    Default Re: Bug in QStringList custom sort

    I guess that would be a lot easier to use a DateEdit or Date/TimeEdit instead of using a QText edit never used it before but thats what is nice about Qt you can google the references for each item =] thats how I have been teaching myself qt the last few weeks. thanks again Wysota

    here is the final code much shorter and easier to use =P
    Qt Code:
    1. //header
    2. #ifndef MAINWINDOW_H
    3. #define MAINWINDOW_H
    4.  
    5. #include <QMainWindow>
    6. #include <QDateEdit>
    7. #include <QPushButton>
    8.  
    9. class MainWindow : public QMainWindow
    10. {
    11. Q_OBJECT
    12.  
    13. public:
    14. MainWindow();
    15.  
    16. private slots:
    17. void buttonPressed();
    18.  
    19. private:
    20. QDateEdit *dateEdit;
    21. QPushButton *button;
    22. QWidget *widget;
    23. };
    24.  
    25. #endif // MAINWINDOW_H
    26.  
    27. //main c++
    28. int dateCount = 0;
    29. QList<QDate> date;
    30. MainWindow::MainWindow(){
    31. dateEdit = new QDateEdit;
    32. button = new QPushButton;
    33. widget = new QWidget;
    34. button->setText("Ok");
    35. QGridLayout *layout = new QGridLayout;
    36. layout->addWidget(dateEdit,0,0);
    37. layout->addWidget(button,0,1);
    38. widget->setLayout(layout);
    39. setCentralWidget(widget);
    40. connect(button,SIGNAL(clicked()),
    41. this,SLOT(buttonPressed()));
    42. }
    43.  
    44. void MainWindow::buttonPressed(){
    45. date << dateEdit->date();
    46. qSort(date.begin(),date.end());
    47. dateCount++;
    48. for(int i = 0; i<dateCount;i++){
    49. cout << date[i].toString("MMMM dd, yyyy").toStdString() << " " << flush;
    50. }
    51. }
    To copy to clipboard, switch view to plain text mode 

    thanks agian appreciate the ideas
    Last edited by giblit; 1st April 2013 at 00:19.

  14. #13
    Join Date
    Feb 2013
    Posts
    71
    Platforms
    Windows
    Thanks
    6
    Thanked 3 Times in 3 Posts

    Default Re: Bug in QStringList custom sort

    I have made it so when it sorts QTreeWidgetItem (column 0 text) it will also sort the corresponding columns text for that item here is the final code that seems to be working, didn't find any bugs in it (in case anyone else is having the same problem I was having.)
    Qt Code:
    1. //header:
    2. #ifndef MAINWINDOW_H
    3. #define MAINWINDOW_H
    4.  
    5. #include <QMainWindow>
    6. #include <QTreeWidget>
    7. #include <QDateEdit>
    8. #include <QPushButton>
    9. #include <QGridLayout>
    10. #include <QMessageBox>
    11.  
    12. class MainWindow : public QMainWindow
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. MainWindow();
    18.  
    19. private slots:
    20. void buttonPressed();
    21. void button2Pressed();
    22. void sortTree();
    23.  
    24. private:
    25. QTreeWidget *treeWidget;
    26. QList<QTreeWidgetItem*> itemList;
    27.  
    28. QDateEdit *dateEdit;
    29. QTimeEdit *timeEdit;
    30. QPushButton *button, *button2, *sort;
    31. QWidget *widget;
    32. QGridLayout *layout;
    33.  
    34. };
    35.  
    36. #endif // MAINWINDOW_H
    37.  
    38.  
    39. //main c++ file
    40. #include <QtGui/QApplication>
    41. #include "mainwindow.h"
    42.  
    43. int main(int argc, char *argv[])
    44. {
    45. QApplication a(argc, argv);
    46. MainWindow w;
    47. w.show();
    48.  
    49. return a.exec();
    50. }
    51.  
    52.  
    53. //mainwindow c++ file
    54. #include "mainwindow.h"
    55. #include <iostream>
    56. #include <QtGui>
    57.  
    58. using namespace std;
    59. QList<QDate> sortedDate;
    60. QList<QDate> regDate;
    61. QList<QTime> time;
    62. QMap<int, int> sortedOrder;
    63. int timeCount = 0;
    64. int dateCount = 0;
    65. MainWindow::MainWindow()
    66. {
    67. layout = new QGridLayout;
    68. dateEdit = new QDateEdit;
    69. timeEdit = new QTimeEdit;
    70. button = new QPushButton;
    71. button2 = new QPushButton;
    72. sort = new QPushButton;
    73. widget = new QWidget;
    74. button->setText("AddDate");
    75. button2->setText("AddTime");
    76. sort->setText("sort");
    77. dateEdit->setDate(QDate::currentDate());
    78. timeEdit->setTime(QTime::currentTime());
    79. connect(button,SIGNAL(clicked()),
    80. this,SLOT(buttonPressed()));
    81. connect(button2,SIGNAL(clicked()),
    82. this,SLOT(button2Pressed()));
    83. connect(sort,SIGNAL(clicked()),
    84. this, SLOT(sortTree()));
    85.  
    86. treeWidget = new QTreeWidget;
    87. treeWidget->setColumnCount(2);
    88. layout->addWidget(treeWidget,0,0);
    89. layout->addWidget(dateEdit,1,0);
    90. layout->addWidget(timeEdit,2,0);
    91. layout->addWidget(button,1,1);
    92. layout->addWidget(button2,2,1);
    93. layout->addWidget(sort, 0, 1,Qt::AlignBottom);
    94. widget->setLayout(layout);
    95. setCentralWidget(widget);
    96.  
    97. }
    98. void MainWindow::buttonPressed(){
    99. regDate << dateEdit->date();
    100. sortedDate << regDate[dateCount];
    101. item = new QTreeWidgetItem(treeWidget);
    102. itemList << item;
    103. itemList[dateCount]->setText(0, regDate[dateCount].toString("MMMM dd, yyyy"));
    104. dateCount++;
    105. }
    106. void MainWindow::button2Pressed(){
    107. if(timeCount < dateCount){
    108. time.removeAt(dateCount-1);
    109. time.insert(dateCount-1,timeEdit->time());
    110. itemList[dateCount-1]->setText(1, time[dateCount-1].toString("h:mm AP"));
    111. timeCount++;
    112. } else if(timeCount == dateCount){
    113. time.removeAt(dateCount-1);
    114. time.insert(dateCount-1,timeEdit->time());
    115. itemList[dateCount-1]->setText(1, time[dateCount-1].toString("h:mm AP"));
    116. }
    117. }
    118.  
    119. void MainWindow::sortTree(){
    120. if(dateCount == timeCount){
    121. qSort(sortedDate.begin(),sortedDate.end());
    122. for(int i = 0; i<regDate.size(); i++){
    123. sortedOrder[i] = sortedDate.indexOf(regDate[i]);
    124. }
    125. treeWidget->clear();
    126. for(int i = 0; i<dateCount; i++){
    127. itemList[i] = new QTreeWidgetItem(treeWidget);
    128. itemList[i]->setText(0, sortedDate[i].toString("MMMM dd, yyyy"));
    129. itemList[i]->setText(1, time[sortedOrder.key(i)].toString("h:mm AP"));
    130. }
    131. } else {
    132. QMessageBox error(QMessageBox::Warning, "ERROR", "Missing Date or Time.");
    133. error.exec();
    134. }
    135. }}
    To copy to clipboard, switch view to plain text mode 
    Last edited by giblit; 2nd April 2013 at 04:02.

Similar Threads

  1. Custom widget table sort
    By giantdragon in forum Qt Programming
    Replies: 10
    Last Post: 10th June 2011, 13:36
  2. Custom Item Delegate Reset, sort of.
    By waynew in forum Qt Programming
    Replies: 3
    Last Post: 1st September 2010, 23:48
  3. QTableWidget custom sort
    By mattc in forum Qt Programming
    Replies: 1
    Last Post: 24th August 2010, 12:01
  4. QTreeWidget (Row Check, Custom Sort)
    By AaronMK in forum Qt Programming
    Replies: 3
    Last Post: 8th January 2008, 16:55
  5. Custom sort with QTableWidget
    By nicolas44 in forum Qt Programming
    Replies: 1
    Last Post: 9th August 2007, 01:47

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
  •  
Qt is a trademark of The Qt Company.