homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

efficient sorting function using javascript

 6:15 am on May 6, 2002 (gmt 0)

Hi all,
I have a prograp in asp. This pgm fetches large number of records from the database & display's these records on the browser in the form of html table. I have a requirement wherein , onclick of the column heading the table should get sorted accordingly. I have a javascript sorting function that responds to the click event & uses bubble sort, but script function takes a lot of time to do the sorting & hence the browser hangs. Can anyone tell me an alternatine sorting function which takes less time to sort records.
Thanks & regards



 12:16 pm on May 6, 2002 (gmt 0)

if you are already using ASP, I would make the click on the column heading another server call, with the appropriate change in the ORDER BY clause of your SQL. You can use directives for this:

<a href="mypage.asp?Sort1">Sort 1</a>

<a href="mypage.asp?Sort2">Sort 2</a>

In your ASP code:
if Request.QueryString == "Sort1" then
sql = sql & " order by field1"
elseif Request.QueryString == "Sort2" then
sql = sql & " order by field2"
end if

Much faaster than trying to do it client side.

I can show you an example if you sticky me.


 12:33 pm on May 6, 2002 (gmt 0)

Thanks for the solution,Can u send me the example? I have a concern. Won't I be hitting the database everytime I sort ,which will reduce the response time.



 12:50 pm on May 6, 2002 (gmt 0)

There is another style of sorting algo called "quicksort" [ciips.ee.uwa.edu.au]. It is faster than "bubble sort" because it works by creating partitions in the data, and sorting within each partition, rather than the inefficient side-by-side sorting of all elements.


 6:01 pm on May 6, 2002 (gmt 0)

I'm not sure how long this URL will last here (we don't generally like to post URLs on WW) but if you can get to it you'll see what I do.


Clicking on a column heading will go back to the db and bring it back in a different sort.

Yes, it takes resources, but the server side processing is generally much faster than client side processing.

(edited by: tedster at 6:15 pm (utc) on May 6, 2002)


 8:10 pm on May 6, 2002 (gmt 0)

I'd suggest you see if you can pre-sort and then cache the result sets based on all of the possible combinations. Superfast response time because there's no need to sort. You'll need to choose an appropriate time period for updating the cached results based upon how often you update the db.


 4:28 am on May 8, 2002 (gmt 0)

Thanks for the URL, I was able to view it. Can u send me the pmlb.asp file coz' I get the following error when I tried to achieve the same

Microsoft OLE DB Provider for ODBC Drivers (0x80040E07)
[Microsoft][ODBC SQL Server Driver][SQL Server]The conversion of char data type to smalldatetime data type resulted in an out-of-range smalldatetime value.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved