Link to home
Start Free TrialLog in
Avatar of mike_marquet
mike_marquet

asked on

Sorting listbox items !

How can I sort the items of a listbox which was not created with LBS_SORT style.

I have try this :

m_MyListBox.ModifyStyle(0, LBS_SORT); // DOESN'T WORK

Can someone help me.

In my application, I have a listbox which is not sorted. A button called "SORT", must sort the listbox items.
Avatar of jhance
jhance

There may be another way that I'm not aware of but what I've done in the past is to just read out all of the items into a temporary location, call ModifyStyle to turn on sort, call ResetContent to clear it out and then finally add all the items back to the control.
m_MyListBox.ModifyStyle(0, LBS_SORT); // DOESN'T WORK

Why doesn't it work?
It does work, it just doesn't re-sort a filled listbox.  At least I've not seen it do that.

You have any ideas?
> It does work, it just doesn't re-sort a filled listbox.  

Exactly.

You could also consider having two boxes; one with LBS_SORT and the other without. Do all operations to both boxes, but only show one depending on whatever makes you toggle.

..B ekiM
Avatar of mike_marquet

ASKER

It doesn't work.

Here's the function :

void MyDlg::OnPB_AlphaSort()
 {
  CStringArray ArrayToSort;

  for (int I=0; I<m_MyListBox.GetCount(); I++)
   {
    CString str;
   
    m_LB_MyListBox.GetText(I, str);
   
    ArrayToSort.Add(str);
   }

  m_LB_MyListBox.ResetContent();

  m_LB_MyListBox.ModifyStyle(0, LBS_SORT);

  for (I=0; I<ArrayToSort.GetSize(); I++)
   {
    CString str = ArrayToSort.GetAt(I);

    m_LB_MyListBox.AddString(str);
   }

  //m_LB_MyListBox.ModifyStyle(LBS_SORT, 0);
 }
ASKER CERTIFIED SOLUTION
Avatar of mikeblas
mikeblas

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
I have make it so :

int compare(const void *arg1, const void *arg2)
 {
  return _stricmp( (char *)arg1, (char *)arg2 );
 }

void MyDlg::OnPB_AlphaSort()
 {
  int  iCount              = m_MyListBox.GetCount();
  char (*ArrayToSort)[255] = new char [iCount] [255];

  memset(ArrayToSort, 0, 255 * iCount);

  for (int I=0; I<iCount; I++)
   {
    CString str;
   
    m_MyListBox.GetText(I, str);

    strcpy(ArrayToSort[I], str);
   }

  qsort( (void *)ArrayToSort, (size_t)iCount, 255, compare);

  m_MyListBox.ResetContent();

  for (I=0; I<iCount; I++)
   {
    m_MyListBox.AddString(ArrayToSort[I]);
   }

  delete [] ArrayToSort;
 }
And the list box was originally created with LBS_HASSTRINGS?
So, then you'll need to create two controls, or recreate the ListBox after you've unloaded it.

..B ekiM
The listbox is create without using owner draw, therefore the hasstring is not accessible but I think in non owner draw mode, the hasstring is set as default.
Is there some reason you don't want to create two controls, mike_marquet?

..B ekiM
No, but it is much easier to use one control and sort or not this only control.
I'm not sure that's true. You'll have to do the sorting maually, since you can't retroactively apply the LBS_SORT style.

Will you be changing the control back to unsorted? What does that mean? Unsorted usually means that there's actually some other order; just that the order isn't alphabetical.

Maybe you should always have LBS_SORT, but do your own item comparison and switch the item comparison routine so that it sorts along the two different orders you like.

..B ekiM
In my application, a have a SORT button. When I push it, the listbox items must be sorted. After it, I can add some more items which will than not be sorted.
You can do that without adding and removing the LBS_SORT style. Just keep the style and manage the comparisons yourself.

..B ekiM
OK, Thanks, I will try it so.
mike_marquet,

These questions are still open and our records show you logged in recently. Please resolve them appropriately as soon as possible. Continued disregard of your open questions will result in the force/acceptance of a comment as an answer; other actions affecting your account may also be taken. I will revisit these questions in approximately seven (7) days. Please note that the recommended minimum for an "Easy" question is 50 points.

https://www.experts-exchange.com/jsp/qShow.jsp?qid=11532221
https://www.experts-exchange.com/jsp/qShow.jsp?qid=11701138
https://www.experts-exchange.com/jsp/qShow.jsp?qid=11841838
https://www.experts-exchange.com/jsp/qShow.jsp?qid=11928102
https://www.experts-exchange.com/jsp/qShow.jsp?qid=11931818
https://www.experts-exchange.com/jsp/qShow.jsp?qid=11996498
https://www.experts-exchange.com/jsp/qShow.jsp?qid=11998538
https://www.experts-exchange.com/jsp/qShow.jsp?qid=12011459
https://www.experts-exchange.com/jsp/qShow.jsp?qid=12039459
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20005590
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20006135
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20071202
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20084832
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20091412
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20093693
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20095151
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20099190
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20099158
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20100338
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20107694
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20114039
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20071199
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20116007
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20128382
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20137131
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20155355
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20192497
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20193603
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20238400
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20239471
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20245231
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20253153

EXPERTS: Please leave your thoughts on this question here.

Thanks,

Netminder
Community Support Moderator
Experts Exchange
Delete request rejected. Comment force accepted.

Netminder
CS Moderator, EE