Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Javascript / Client Side Development

Sorting div contents using javascript


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>

On May 10, 11:06 am, rrowles2000 <rich@rowles.co.uk> wrote:

> 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[i][field]<array[j][field])// swap them

}

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

and generate the html by iterating through the array?

---
Geoff

In comp.lang.javascript message <1178809598.713879.30@q75g2000hsh.goo
glegroups.com>, Thu, 10 May 2007 08:06:38, rrowles2000
<rich@rowles.co.uk> posted:

>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.

--
 (c) John Stockton, Surrey, UK.  ?@merlyn.demon.co.uk   Turnpike v6.05   IE 6
 news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
 <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
 <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc