Sorting a ListBox

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: May 2008
Posts: 5
Reputation: coderoobie is an unknown quantity at this point 
Solved Threads: 0
coderoobie coderoobie is offline Offline
Newbie Poster

Sorting a ListBox

 
0
  #1
Aug 15th, 2008
Hi happy people

I've bin trying to sort items in a list box, the problem is obviously: It doesn't work

Please, any modifications to the code below is fully and unconditionally allowed.

  1. void __fastcall TForm1::btnSortClick(TObject *Sender)
  2. {
  3.  
  4. //sort ascending
  5. if (rdAscending->Checked){
  6. bool swopped = true;
  7. int iEnd;
  8. int arr = lstWords->Count;
  9. //ShowMessage(arr);
  10. iEnd = arr -1;
  11.  
  12. while (swopped)
  13. {
  14. swopped = false;
  15. for (int c=0; c < iEnd; c++)
  16. {
  17. AnsiString a = lstWords->Items->Strings[c];
  18. AnsiString b = lstWords->Items->Strings[c+1];
  19. if (a > b)
  20. {
  21. AnsiString txt = lstWords->Items->Strings[c];
  22. lstWords->Items->Strings[c] = lstWords->Items->Strings[c+1];
  23. lstWords->Items->Strings[c+1] = txt;
  24. swopped = true;
  25. iEnd--;
  26.  
  27. }
  28.  
  29. }
  30. }
  31.  
  32. 'Let us do what we do best!'
  33. }
  34. //sort descending
  35. else if (rdDescending->Checked){
  36. bool swopped = true;
  37. int iEnd;
  38. int arr = lstWords->Count;
  39. iEnd = arr -1;
  40.  
  41. while (swopped)
  42. {
  43. swopped = false;
  44. for (int c=0; c < iEnd; c++)
  45. {
  46. AnsiString a = lstWords->Items->Strings[c];
  47. AnsiString b = lstWords->Items->Strings[c+1];
  48. if (a < b)
  49. {
  50. AnsiString txt = lstWords->Items->Strings[c];
  51. lstWords->Items->Strings[c] = lstWords->Items->Strings[c+1];
  52. lstWords->Items->Strings[c+1] = txt;
  53. swopped = true;
  54. iEnd--;
  55.  
  56. }
  57.  
  58. }
  59. }
  60. }
  61. else
  62. ShowMessage(
  63. "Select either [Ascending] or [Descending]! \nPlease Try Again!");
  64. }
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 374
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: Sorting a ListBox

 
0
  #2
Aug 15th, 2008
Can't you use qsort() for this? It'd make the code a lot shorter... and easier I guess.

And a, a bit more precise about the error? What are you getting? Segfaults or just a not-sorted listbox?
Last edited by Clockowl; Aug 15th, 2008 at 6:53 am.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 5
Reputation: coderoobie is an unknown quantity at this point 
Solved Threads: 0
coderoobie coderoobie is offline Offline
Newbie Poster

Re: Sorting a ListBox

 
0
  #3
Aug 15th, 2008
I'l try using 'qsort()'. I never new there was such a function, but now because of you I..........! ;-)

About the error, it was not an error at all. It is wrong logic that I used, and I got it al fixed!

Thanks a lot clockowl!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC