How to programatically execute a Listbox sort using comparerows?

Hello all,

Following population of a listbox, I would like to sort it using code in the CompareRows event. Can anyone tell me how to do this?

Thanks,
Tim

Have you studied the examples in the docs?

ListBox.CompareRows

Hello Eli,

Yes, I have. The problem is " If you implement the event, it gets called during a ListBox sort, e.g., when a user clicks in the header area." So how to programatically simulate “when a user clicks in the header area.” ?

Tim

Listbox.Sort?

Example from the LR.

ListBox1.ColumnsortDirection(0)=ListBox.SortDescending
ListBox1.SortedColumn=0   //first column is the sort column
ListBox1.Sort

Hi Albin!

I had tried that. Did nothing.

I’ll check it again to be sure, but it did not work before I did the post…

Nice to hear from you, and thanks for your reply.
Tim

Strange. Did you try both ascending and descending? SortColumn was the right column? :slight_smile:

Sort col is the left most - col 0.
The parameters
ListBox1.ColumnsortDirection(0)=ListBox.SortDescending
ListBox1.SortedColumn=0 //first column is the sort column

are preset during the fill process. I am trying to get the correct sort in the method that does the filling, just before it returns. Its late - I will recheck this again with fresh eyes tomorrow.

Thanks for the help and reply!
Tim

Albin’s answer is correct.
Alternatively you can simulate a click in the header by the user: Listbox.PressHeader

Hi Everyone.
I retried the code Albin suggested. With fresh eyes I found my simple mistake - it does work! Thanks Albin.

Eli’s suggestion is simpler and better since it can be used to sort by whichever column is needed to be sorted. So I think I will use that one.

Thanks for the help!
Tim