Link to home
Start Free TrialLog in
Avatar of hpet
hpet

asked on

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();
}
Avatar of PaulCaswell
PaulCaswell
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of hpet
hpet

ASKER

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
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
use vector or list and sort it

list<list<char>> a;
.......
store the values to it
.......
a.sort();
-Mahesh
Avatar of hpet

ASKER

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
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
ASKER CERTIFIED SOLUTION
Avatar of dog_
dog_

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hpet

ASKER

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