Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sorting a multidimentional array in javascript 1

Status
Not open for further replies.

kirankumar

Programmer
May 17, 2000
25
US
Hi,<br>I want to store the records in a javascript array, so that i can do the client side processing like sorting on a different column or field. I am able to store the records retrieved from database in an array. But i am not able to sort it. Somewhere it's going wrong. If you have any logic for sorting a multidimentional array, please give it to me. <br>I appreciate your help.<br><br>Kiran
 
How do you have to sort them... could you possibly sort each individual array in the multidimensional array<br><br><A HREF="mailto:jared@aauser.com">jared@aauser.com</A>
 
Dear Kiran,<br><br>Sorting in Javascript is fairly simple you don't need an algorithm since it is already supplied for you.<br><br>Since you havn't provided any code describing what your problem is it is hard to know how to answer.<br><br>The javascript sort method takes two parameters one is the array to be sorted and the other is the sort function. The sort function returns -1,0 or 1 describing the evaluation of the two elements. Fairly simple.<br><br>-pete
 
Dear Pete,<br>I cannot give you the code because it's too lengthy.But i can explain the situation.<br>1. I am selecting ssn, fname, lname from a table from my asp page.<br>2. I am storing that recordset in a javascript array.<br>3. I am able to dispaly them in the form of a table.<br>4. The array is by defalut sorted on ssn.<br>5. All header fields are clickable.<br>6. What i need to do now is when the user clicks on the header part of the table, let us say fname, the order of display must change.<br><br>The displayed table will look similar to this.<br><br>ssn&nbsp;&nbsp;&nbsp;&nbsp;fname&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lname<br>111&nbsp;&nbsp;&nbsp;&nbsp;abc&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xyz<br>1111&nbsp;&nbsp;&nbsp;ghjghj&nbsp;&nbsp;&nbsp;&nbsp;ghgkg<br>22222&nbsp;&nbsp;ghgkgh&nbsp;&nbsp;&nbsp;&nbsp;gjhggg<br><br>If you wnat to look at the code i can send it to you as an attachement if you give me your mail.<br><br>Thanks<br>Kiran<br>
 
Dear Kiran,<br><br>Normally this type of operation is done using a round trip to the server and re-generating the page with the new sort order. In that scenario you use your database to perform the sort order for you.<br><br>If you want to re-order the table in client side script code you will have all sorts of browser compatibility problems. If you can limit the browser to IE 4+ then you can do it but the code will be much more complex than using the server side code to order the rows and re-generate the page.<br><br>Let me know what you think<br>-pete
 
Dear pete,<br>I have already done most of the things. Only thing i need to do now is a logic to sort the array. Forget about the javascript and all those things.Just give me the logic to sort an array(an alogorithm).I will try to implement the same in javascript.<br><br>Thanks <br>Kiran
 
Dear Kiran,<br><br>As I stated above Javascript has a sort algorithm built in! You don't want to code your own. Here is a server side script that sorts an array and displays the results. <br><br>&lt;script language=javascript runat=server&gt;<br>function mysort( eleA, eleB){<br><br>&nbsp;&nbsp;return eleA - eleB;<br>}<br>&lt;/script&gt;<br>&lt;%<br>&nbsp;&nbsp;var myArray = new Array( 11, 7, 21, 14, 9, 33, 5);<br>&nbsp;&nbsp;var sortedArray = myArray.sort( mysort);<br><br>&nbsp;&nbsp;for(i=0; i&lt;sortedArray.length; i++)<br>&nbsp;&nbsp;&nbsp;&nbsp;Response.write( sortedArray<i> + &quot;&lt;br&gt;&quot;);<br>%&gt;<br><br>However you should be using your database to sort your rows it is much faster than sorting in ASP.<br><br>&quot;But, that's just my opinion... I could be wrong&quot;.<br>-pete
 
Dear Pete,<br>Thanks a lot.<br>I am working on it.<br><br>Kiran
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top