Sorting div contents using javascript

R

rrowles2000

Hi,

I have four chuncks of html and I'm trying to sort them. see this
link. Basically the sort works for alphabetic sort but not for
numerics. Any ideas very welcome? I just can't see what I'm doing
wrong!!! Cheers

http://cricketclub.wherwell.net/bowling-averages-2007.htm

each chunk looks like this...

<div id="player_1" class="player">
<table>
<tr>
<td>1</td>
<td><div class="playerName" id="playerName_1">d</div></td>
<td><div class="overs" id="overs_1">10</div></td>
<td><div class="maidens" id="maidens_1">0</div></td>
<td><div class="runs" id="runs_1">50</div></td>
<td><div class="wickets" id="wickets_1">90</div></td>
<td>
<div class="averageRuns" id="averageRuns_1"></div>
<script language="javascript">
document.getElementById('averageRuns_1').innerHTML =
Math.round((parseInt(document.getElementById('runs_1').innerHTML) /
parseInt(document.getElementById('wickets_1').innerHTML))*100)/100;
</script>
</td>
</tr>
</table>
</div>


And the code is here.


<script language="javascript">

function quickSort(prefix,htmlItem,t)
{
var n =1;
while (n < getArrayLength(prefix))
{
var makeSwap = false;
if (t == 'c')
{
if((sv(prefix + n)) > (sv(prefix + (n+1))))
{makeSwap = true;}
}
if (t == 'l')
{
if(parseLong(sv(prefix + n)) > parseLong(sv(prefix + (n
+1))))
{makeSwap = true;}
}
if (t == 'i')
{
if(parseInt(sv(prefix + n)) > parseInt(sv(prefix + (n
+1))))
{makeSwap = true;}
}
if(makeSwap)
{
document.getElementById(prefix + n).style.background
= '#ccc';
document.getElementById(prefix + (n
+1)).style.background = '#ccc';
swapHTML(htmlItem+n, htmlItem+(n+1),n);
alert('Pause');
document.getElementById(prefix + n).style.background
= '#fff';
document.getElementById(prefix + (n
+1)).style.background = '#fff';
}
n++;
}

}


function swapHTML(a,b,f)
{
alert('a:'+a+' b:'+b+' n:'+f);
var aHTML = document.getElementById(a).innerHTML;
var bHTML = document.getElementById(b).innerHTML;
var cHTML = document.getElementById(b).innerHTML;

document.getElementById(b).innerHTML = aHTML.replace('_'+(f),'_'+(f
+1));
document.getElementById(a).innerHTML = cHTML.replace('_'+(f+1),'_'+
(f));
}

function getHTML(elementName)
{
return trim(document.getElementById(elementName).innerHTML);
}

function trim(str)
{
return ltrim(rtrim(str));
}

function sv(elementName)
{
return document.getElementById(elementName).innerHTML;
}


function getArrayLength(prefix)
{
var m=1;
var elementId = prefix + m;
while (idExists(elementId))
{
m++;
elementId = prefix + m;
}
return m-1;
}

function idExists(elementId)
{
if (document.getElementById(elementId) == null)
{return false;}
else
{return true;}
}
</script>
 
G

Geoffrey Summerhayes

Hi,

I have four chuncks of html and I'm trying to sort them. see this
link. Basically the sort works for alphabetic sort but not for
numerics. Any ideas very welcome? I just can't see what I'm doing
wrong!!! Cheers


function quickSort(prefix,htmlItem,t)

<*snip*>

Don't like the name. There is a very well known sort algorithm
called quicksort and this is not it. What you have is an
incomplete implementation of a bubble sort.
function swapHTML(a,b,f)
{
alert('a:'+a+' b:'+b+' n:'+f);
var aHTML = document.getElementById(a).innerHTML;
var bHTML = document.getElementById(b).innerHTML;
var cHTML = document.getElementById(b).innerHTML;

document.getElementById(b).innerHTML = aHTML.replace('_'+(f),'_'+(f
+1));

I suspect you wanted:

aHTML.replace(new RegExp('_'+(f),'g'),'_'+(f+1));

But why not just have an array of objects like

{playerName:'d', overs:10, maidens:0, runs:50, wickets:90}

sort the array

function sort(array,field)
{
...
if(array[field]<array[j][field])// swap them
}

i.e. sort(teams,'maidens');

and generate the html by iterating through the array?
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
glegroups.com>, Thu, 10 May 2007 08:06:38, rrowles2000
I have four chuncks of html and I'm trying to sort them. see this
link. Basically the sort works for alphabetic sort but not for
numerics. Any ideas very welcome? I just can't see what I'm doing
wrong!!! Cheers

Can you not use the .sort() method of Array? .sort can take a
comparison function.

But if alphabetic sort works but not numeric, a quick fix would be to
add say five million to each number before sorting and remove it after.

You seem to be using the page itself for data storage and sorting on
that. Better, I think, to have the data as an array of player objects,
sort it, and write it out when completed. Crude example :

var Squad = [
{Name: "Fred" , Age: 93 , Average : 3.7 },
{Name: "Bert" , Age: 23 , Average : 2 },
{Name: "Alex" , Age: 7 , Average : 48 }
]

function NameComp(A, B) { var a = A.Name, b = B.Name
return A>B - B>A }

function AgeComp(A, B) { return A.Age - B.Age }

function AveComp(A, B) { return A.Average - B.Average }

Squad.sort(NameComp)

function WriteChap(X) {
document.getElementById("Divn").innerHTML +=
X.Name + " " + X.Age + " " + X.Average + "<br>"}

for (var j=0 ; j<Squad.length ; j++) WriteChap(Squad[j])


It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top