Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 20th, 2003, 12:08 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: High Wycombe, UK, United Kingdom.
Posts: 344
Thanks: 0
Thanked 1 Time in 1 Post
Default 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.
__________________
All the best

Greg
------------------
Greg Griffiths
Web & Livelink Developer
http://www.greggriffiths.org
Reply With Quote
  #2 (permalink)  
Old June 20th, 2003, 01:10 PM
Authorized User
 
Join Date: Jun 2003
Location: Gainesville, FL, USA.
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sorting member XSLT 25 July 13th, 2007 08:44 AM
SORTING pallone XSLT 3 October 29th, 2006 08:45 AM
Sorting sunny76 Excel VBA 2 September 19th, 2005 09:31 PM
Datagrid sorting by non alphabetical sorting? LLAndy VS.NET 2002/2003 1 July 15th, 2004 01:20 AM
Sorting? pbernardo XSLT 2 October 27th, 2003 11:34 AM



All times are GMT -4. The time now is 12:34 PM.


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