p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Javascript How-To (http://p2p.wrox.com/forumdisplay.php?f=87)
-   -   sorting (http://p2p.wrox.com/showthread.php?t=783)

Greg Griffiths June 20th, 2003 12:08 PM

sorting
 
I have a several columns of data coming back from a query that I present to the user, I would like to be able to allow the user to sort this as they see fit.

Initially I've used an array and the SORT function, but the requiements are now to allow sorting of the following nature :

sort by col1 ascending then col2 descending

This is a lot more complex than just a simple SORT call. Some people have suggesting using XML for the data and the sorting, but I've not used it heavily, others have suggested building a DB call each time, but I'd prefer to do it client side.

I'd appreciate any thoughts or comments from the group.

Olorin June 20th, 2003 01:10 PM

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
FirstNames
LastNames
Balances
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:
--pseudocode
Code:

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)
    }

/--pseudocode

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).

HTH,
Olorin


All times are GMT -4. The time now is 05:23 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.