help with my 2D array function

Thread: help with my 2D array function

1. Senior Member
Join Date
Dec 1969
Posts
353

help with my 2D array function

This function is for sorting a 2D array which is an array of columns or fields, not records or rows. The problem is that its only sorting one column, not the whole record. I&#039;ve marked the line which I think is the problem.<BR><BR>[code language="C#"]<BR>function SortTwoDimensionalArray(a,column,order) // a is array, column is column to sort by.<BR>// This is for an unconventional 2 dimensional array, which is an array of columns (fields), not rows (records)<BR>{<BR> var aSorted, aConverter, i, j;<BR> // aSorted will contain the sorted array; aConverter will contain an array for working out<BR> // how to keep records together; i and j are iterators.<BR><BR> aSorted = new Array(a.length);<BR><BR> aSorted[column] = a[column].sort(); // Sort the array containing the column to be sorted by<BR><BR> if (aSorted[column] == a[column])<BR> return a;<BR><BR> // This double loop pattern then matches elements of the sorted array with corresponding elements of the<BR> // origional array, recording the results in a converter array<BR> for (i = 0; i &#060; aSorted[column].length; i++)<BR> {<BR> for (j = 0; j &#060; aSorted[column].length; j++)<BR> {<BR> if (aSorted[i][j] == a[i][0])<BR> {<BR> aConverter[i] = j;<BR> break;<BR> }<BR> }<BR> }<BR><BR> // This double loop uses aConverter to decide which element to assign each value to in aSorted<BR> for (i = 0; i &#060; aSorted.length; i++)<BR> {<BR> if (i != column)<BR> for (i = 0; i &#060; aSorted.length; i++)<BR> aSorted[i][j] = a[i][aConverter[i]]; // I think this is the problem<BR> }<BR><BR> return aSorted;<BR>}<BR>[/code]

2. Senior Member
Join Date
Dec 1969
Posts
19,082

RE: help with my 2D array function

well, for one thing you&#039;ve nested two loops which use the SAME iterator. shouldn&#039;t the inner loop use &#039;j&#039; as an index as the previous one does?<BR><BR>or is my hangover misleading me again?

3. Senior Member
Join Date
Dec 1969
Posts
353

RE: help with my 2D array function

oh, yes, so I have. Sometimes this sort of thing takes a second pair of eyes. I swithed the i for j, which is what I intended. Its still not working though. This is what I use to test it:<BR><BR>
Code:
`<BR>document.write(&#039;&#060;TABLE&#062;&#039;);<BR><BR>for (i = 0; i &#060; ArrayToSort[0].length; i++)<BR>{<BR>	document.write(&#039;&#060;TR&#062;&#060;TD&#062;&#039;<BR>				+	ArrayToSort[0][i] + &#039;&#060;/TD&#062;&#060;TD&#062;&#039;<BR>				+	ArrayToSort[1][i] + &#039;&#060;/TD&#062;&#060;TD&#062;&#039;<BR>				+	ArrayToSort[2][i] + &#039;&#060;/TD&#062;&#060;TD&#062;&#039;<BR>				+	ArrayToSort[3][i] + &#039;&#060;/TD&#062;&#060;/TR&#062;&#039;);<BR>}<BR><BR>document.write(&#039;&#060;TR&#062;&#060;TD COLSPAN=4&#062;<BR><BR><BR>&#060;/TD&#062;&#060;/TR&#062;&#039;);<BR><BR>SortTwoDimensionalArray(ArrayToSort,0);<BR><BR>for (i = 0; i &#060; ArrayToSort[0].length; i++)<BR>{<BR>	document.write(&#039;&#060;TR&#062;&#060;TD&#062;&#039;<BR>				+	ArrayToSort[0][i] + &#039;&#060;/TD&#062;&#060;TD&#062;&#039;<BR>				+	ArrayToSort[1][i] + &#039;&#060;/TD&#062;&#060;TD&#062;&#039;<BR>				+	ArrayToSort[2][i] + &#039;&#060;/TD&#062;&#060;TD&#062;&#039;<BR>				+	ArrayToSort[3][i] + &#039;&#060;/TD&#062;&#060;/TR&#062;&#039;);<BR>}<BR><BR>document.write(&#039;&#060;/TABLE&#062;&#039;);<BR>`
<BR><BR>If it has worked, the first row should read 0 4 8 12, the second 1 5 9 13, and so on.

4. Senior Member
Join Date
Dec 1969
Posts
96,118

Pardon me, but this is *nutso*

You are using JavaScript!<BR><BR>Which has a BUILT IN sort capability for arrays!<BR><BR>And since there isn&#039;t any such thing as a "2D array" in JS, you just have to ask it to sort your "array-of-arrays".<BR><BR>And that is *DIRT EASY*.<BR><BR>Here...a working demo...worked first time after I cleaned up 2 typos.<BR><BR>**************<BR>&#060;HTML&#062;&# 060;BODY&#062;<BR><BR>&#060;SCRIPT&#062;<BR>var demo = new Array(<BR> new Array( &#039;Abbie&#039;, &#039;Trent&#039;, 27 ),<BR> new Array( &#039;Bill&#039;, &#039;Smith&#039;, 42 ),<BR> new Array( &#039;Carol&#039;, &#039;Roberts&#039;, 33 ),<BR> new Array( &#039;Dean&#039;, &#039;Quester&#039;, 23 ),<BR> new Array( &#039;Everett&#039;, &#039;Porter&#039;, 50 )<BR> );<BR><BR>var sortColumn = 0;<BR><BR>function arraySort( elem1, elem2 )<BR>{<BR> if ( elem1[sortColumn] &#062; elem2[sortColumn] ) return 1;<BR> if ( elem1[sortColumn] &#060; elem2[sortColumn] ) return -1;<BR> return 0;<BR>}<BR><BR>function show( )<BR>{<BR> // have to set a global, unfortunately<BR> sortColumn = parseInt( document.TheForm.column.value );<BR> <BR> var sorted = demo.sort( arraySort );<BR><BR> html = "&#060;TABLE Border=3 CellPadding=8&#062;"<BR> for ( var row = 0; row &#060; sorted.length; ++row )<BR> {<BR> html += "&#060;TR&#062;&#060;TD&#062;" + sorted[row].join("&#060;/TD&#062;&#060;TD&#062;") + "&#060;/TD&#062;&#060;/TR&#062;"<BR> }<BR> html += "&#060;/TABLE&#062;";<BR> document.getElementById("showme").innerHTML = html;<BR>}<BR>&#060;/SCRIPT&#062;<BR><BR>&#060;FORM Name="TheForm"&#062;<BR>Choose a column: <BR>&#060;SELECT Name="column"&#062;<BR> &#060;OPTION value=0&#062; First name<BR> &#060;OPTION value=1&#062; Last name<BR> &#060;OPTION value=2&#062; Age<BR>&#060;/SELECT&#062;<BR>Then click &#060;INPUT Type=Button Value="HERE" onClick="show();"&#062;<BR>&#060;/FORM&#062;<BR>&#060;HR&#062;<BR>&#060;DIV ID="showme"&#062;&#060;/DIV&#062;<BR>&#060;/BODY&#062;&#060;/HTML&#062;<BR>

5. Senior Member
Join Date
Dec 1969
Posts
96,118

Here's the same thing...

...done fancier, with data from a DB, using an array of objects instead of an array of arrays (though an array *is* an object in JS, so and array of arrays *IS* an array of objects...there will be a quiz Friday).<BR><BR>Oh, and using frames, so that it works even with NS 4 (and, I think NS 3!) browsers.<BR><BR>

6. Senior Member
Join Date
Dec 1969
Posts
96,118

Surprised you didn't...

...catch this one! Why use for loops *at all* to sort any kind of array in JavaScript?<BR><BR>&#060;tch/&#062;<BR><BR>

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•