Possible to exclude/lock some rows from a sort?

Hi Folks,

I have a listbox that has sortable data in a column, but the last two cells shouldn’t be included in the sort. Can this be accomplished automatically, or must I create a custom sort?

[quote=88502:@Tim Jones]Hi Folks,

I have a listbox that has sortable data in a column, but the last two cells shouldn’t be included in the sort. Can this be accomplished automatically, or must I create a custom sort?[/quote]

Why not

  • store the two rows (cells) content in an array
  • remove the last two rows
  • sort
  • addrow the stored values

Thanks, Michel. That’s where I was going, but then I remember the compareRows method and I can just ignore the two rows in the sort. However, I may test the two and see if your way is faster when there are a high hundred (750+) entries in the list.

Actually, the store / remove in the SortColumn event is very fast.

Which brings up the next obvious question -

What event fires after the SortColumn event finishes? Where do I add the two stored / removed rows back into the list?

There is no event, so, off the top of my head, you have two options:

  • Within the SortColumn event, set a flag, then Sort again. In the next recursion of SortColumn, check the flag and let the sort proceed by returning False. When it gets back to the first recursion, add back your rows and return True to stop any further sort.
  • In the SortColumn event, start a Timer that will add back the last two rows. Since the Timer won’t fire until after the sort is completed, it will look seamless to the user.

Actually, if it works, I’d turn the first idea into a subclass that raises an AfterSort event.

Actually, I found that doing it all in the HeaderPressed event gets what I want and is very fast:

Check the rows and save / remove
set sortedColumn
set ColumnSortDirection
call Me.Sort
Add the saved rows back to the bottom
return True <<-- That’s the magic sauce in this case.

(man, I wish there was a call hierarchy for each control so that we know what was going on instead of sprinkling a bunch of no-op procedures into all of the events to determine the path … )

[quote=88536:@Tim Jones]call Me.Sort
[/quote]

That will sort the entire rows. Unless your listbox has only one column, it does not work like a column sort by press header.

Here is how I do it :

  • I store and remove the last two rows in HeaderPressed
  • In SortColumn I set the timer.mode to Timer.ModeSingle - The timer has a period of 10 and is set in the IDE to off.
  • In the Action event of the timer, I add back the two rows

If the user clicks again in the header, it sorts the other way but the two cells remain last.

In this case, it is only a one column list :).

So it works fine. :slight_smile:

I don’t even know what this means. It always sorts the entire listbox. Otherwise, your data would get way out of sync.

And yes, it should work exactly like the user pressing the header.

[quote=88542:@Tim Hare]I don’t even know what this means. It always sorts the entire listbox. Otherwise, your data would get way out of sync.

And yes, it should work exactly like the user pressing the header.[/quote]

By the LR and the name of the event, I had understood that the header click sorted only the column. “The user has clicked on the passed column header to sort that column”. Somehow, it should read instead “The user has clicked on the passed column header to sort the listbox based on that column”.

You are right, the header click sorts the entire listbox, based on the clicked column. So as long as SortedColumn is right, Sort does the same as clicking the header.

Spreadsheets allow sorting of one column. So it is not as illogical as it sounds.

Fair point. Although I’ve always found that behavior irritating. Most of the time, the values of the other columns are related to the column being sorted and I always want them to stay with the column. It is rare (in my experience) to want to sort just the values of a single column.

At any rate, it is not readily possible in a listbox, then the issue is gone :slight_smile:

Use a “hidden” column (widhts = 0) for sorting.
Fill additional this column for every row.
For the last rows fill in very high-values.
In the HeaderPressed event, sort this hidden column