As WebListBox does not have native sort capability as Desktop ListBox, so I am looking for a solution for this. Add SQL Order By is a common practice but my data set comprises from a few tables and there is conditional display on a column (if a then show tableA.price, else show tableB.price on the same cell), so setting SQL order by may be too complex to me.
So I am looking for a “more generic” solution, after finish filling the WebListBox, build a variant 2D array to hold all the values. Sort the array according to users click on WebListBox header, then refill the WebListBox again. The problem is, how can I sort a 2D array base on column selected?
I prefer to define a structure that corresponds to what I am displaying in a row, and create a single dimension array of type “structure”. I can then compare a member of the structure between array elements and reassign array elements. There are surely more elegant solutions, but this one does work.
Louis, do you mean to create a class that have properties corresponding to the column list? Seems that changing the colum list needs to change the class also and this can be replicated to other weblistboxes. If, I mean if, there is a way to sort a 2D array, this sorting method can be generic, and applies to any modules in my app.
Tony, no not a class. That too would work however.
In a module, define a structure with members corresponding to the data that you are displaying in the listbox. In the page where the weblistbox exists (or webcontainer…) , define an array typed with the structure created before. You have a single dimension array. Each element of the array has each of the members defined in the stucture. Say the structure is defined like this:
in a module:
Structure Person
firstname as string * 100
lastname as string * 100
age as integer
and then your array in the page:
dim prople() as person
You can set
redim person(5) or whatever you normally do with an array
people(0).firstname = “Barney”
people(0).lastname = “Rubble”
people(0).age = 55
and so on. Then, you can create code to compare any of the members of the array. You can therefore sort your array by comparing people(x).age with people(X+1).age and reassign people() in order of people().age, for example. You create a second variable of type person to temporarily hold the array element that you are reassigning. That’s pretty much all. You can use and adapt pretty much any sort algorithm that you want .