Thread: sorting
View Single Post
  #2 (permalink)  
Old June 20th, 2003, 01:10 PM
Olorin Olorin is offline
Authorized User
Join Date: Jun 2003
Location: Gainesville, FL, USA.
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts

My Cheap $0.02:
I was talking about a similar situation with a colleague. Our final thoughts were that
yes, it would be nice to do sorting on the client-side, BUT to sort a whole recordset would require that you send to the client the whole recordset.
For instance, say you're accessing a list of customers, and that by default they are ordered (in alphabetical order) by the first column (i.e. the First Name). Furthermore, assume that you are displaying 10 customers at the time on the screen. This way, you'll send the first 10 customer records to the client, where they are displayed together with a "Next 10 customers" and a "Previous 10 customers" buttons.
Fine. Now the user clicks on the "Last Name" header to sort the customers by last name. All of a sudden, the first customer (by last name) is a customer that did not appear in those first 10 customers when they were ordered by First Name...

In other words: sorting on the client side saves you a trip to the server every time you need to re-sort, but requires you to send the whole recordset to the client (which, potentially, is a Very Large set, and could take a Long long time to load).
So, it turns out that the right answer is: it depends (surprised?).
If you know that your recordset will always be small, or if you want to resort only the records that were currently presented on the screen, client-side sorting could be the right answer. Otherwise, you may want to keep the sorting on the server.

If you are fine with going back to the server to re-sort, you will simply use the ORDER BY clause of the SQL statements that I am assuming you'll use to get the data from the DB. It is relatively simple to implement, and lets you rely on the DB technology to do the sorting (hei, why re-invent the wheel ?).

If, on the other hand, you decide in favour of client-side sorting, then you can use a sorting script. I am not sure if Jscript lets you declare and use complex data structures (like structs and such), but even if it doesn't let you do that, you'll just have to store your data in parallel arrays.
So, for instance, you could have
being three arrays where the entry at index i in FirstNames is the first name of the customer with last name in LastNames[i] and balance in Balances[i].
Once you have this going on, of course, how do you sort?
Ah, well, there's a bunch of sorting algorithms you could use. The simplest one, of course, is Bubble Sort.
Doing a Bubble sort on a single array, or multiple parallel arrays is the same, except that, when you have multiple arrays, if you swap two items, you need to swap the corresponding items in all arrays as well.
For instance:
LET L be the highest index in your three arrays
for (int i=0; i<L; i++)
  for(int j=i; j<L; j++)
    if(NeedSwap(i, j))
      SwapFirstNames(i, j)
      SwapLastNames(i, j)
      SwapBalances(i, j)

Here you are free to define what it means that you NeedSwap(i, j).
For instance, if the user selected to sort in ascending order by last Name, you'd pick the LastNames[i] and LastNames[j] entries and see if the LastNames[i] entry is "greater than" the LastNames[j] entry. If so, you need to swap them. and so on.

The SwapFirstNames, SwapLastNames and SwapBalances routines should simply do a swap of the
entries indexed i and j in the appropriate array.

Hope I didn't bore you,
and that this will give you a quick start.
BTW, Bubble Sort is one of the slowest sorting algorithms out there,
so if you are looking for fast performance, check out other alternatives (MergeSort, QuickSort and such).

Reply With Quote