• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 480
  • Last Modified:

Sorting list control

Hello,

I have very basic knowledge in C/C++ and I need your help.
This shouldn't be hard for you.

I have one column list control and I would like items to appear sorted A-Z (ascending).
At first I started with SortItems and callback function.
Only first item seems to compare (dunno why, perhaps I did something wrong) as many as there is elements in the list.
For example: a,b,c,d
Compare function repeats comparing a=a.

Also, what about alternative to use qsort?

This is code I have at the moment:

// application-defined comparison function
static int CALLBACK MyCompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
    // pointer to list object
    CMyListBox *pList = (CListCtrl*) lParamSort;
   
    // first item text to compare
    CString strItem1 = pList->GetItemText(lParam1, 0);
 
    // second item text to compare
    CString strItem2 = pList->GetItemText(lParam2, 0);
 
    // compare both items and return:
    //    - negative if item1 < item2
    //    - zero if item1 == item2
    //    - positive if item1 > item2
    return strcmp(strItem2, strItem1);
}
 
void CEditObjectPageTree: nTreeAddTree()
{
 CMyListBox *pList = (CMyListBox*)GetDlgItem(IDC_TREE_TREES);
 
 // sort listbox items with "application-defined" comparison function
 pList->SortItems(MyCompareFunc, (LPARAM) pList);
 
SelectControls();
}
0
hpet
Asked:
hpet
  • 3
  • 2
  • 2
  • +1
1 Solution
 
PaulCaswellCommented:
hpet,

The code looks reasonable! I presume you know that it is always ciomparing a=a because you have put log statements in. What is being returned by the '   return strcmp(strItem2, strItem1);'?

You may find a better response if this question was in C++. Would you like me to move it there?

Paul
0
 
hpetAuthor Commented:
Thanks Paul for your quick response.
Yes please, if possible move question to c++ area.
Wherever the response will be better :)

In debug window I monitor variables and there I can see it always comapre a=a which always gives result of equal.

thanks,
Peter
0
 
PaulCaswellCommented:
Moved to C++ as requested.

I am not to up-to-date with C++ but I'd have thought that if your compare routine is being given two pointers to the same item than I'd guess the list is malformed. Would you post your list building code? If you dont sort the list, does it display correctly?

Paul
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
smpoojaryCommented:
use vector or list and sort it

list<list<char>> a;
.......
store the values to it
.......
a.sort();
-Mahesh
0
 
hpetAuthor Commented:
smpoojary,

can you be please more specific on how to combine my problem with your solution?

Otherwise, yes, the items are listed (just not sorted)

Pet
0
 
smpoojaryCommented:
Pet,
I don't know what is the structure of the  CMyListBox. Do one thing go though the values of  pList  and store those value to list<string> a; and sort it a.sort(); See following sample code

#include <iostream>
#include <string>
#include <list>
using namespace std;

int main()
{
      list<string> a;

      a.push_back("mahesh");
      a.push_back("dinesh");
      a.push_back("mahesh");
      a.push_back("suresh");
      a.push_back("mahesh");
      a.push_back("chandra");

      a.sort();

      list<string>::iterator itr;

      for(itr = a.begin(); itr != a.end(); ++itr)
      {
            cout << *itr << endl;
      }

      return 0;
}

-Mahesh
0
 
dog_Commented:
The CListCtrl class has a sort option. Click on the control and go to properties and set the sort option to true.
0
 
hpetAuthor Commented:
Thank you all for great help.
I did noticed Sort dropdown in properties dialog before but whenever I chose it program crashed on running. I did start new project then I program didn't crash anymore. Seems like first project memorized some functions I removed already.

Anyway, it works now and thanks others for pointing out different solutions!

Pet
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now