# Dynamic content - resorting arrays

**URL:** <https://forum.kirupa.com/t/dynamic-content-resorting-arrays/51335>\
**Category:** web dev\
**Created:** [May 12, 2004, 3:33am UTC](https://forum.kirupa.com/t/dynamic-content-resorting-arrays/51335 "2004-05-12T03:33:36Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![ironikart](https://avatars.discourse-cdn.com/v4/letter/i/7feea3/32.png) [@ironikart](https://forum.kirupa.com/u/ironikart)\
**Post date:** [May 12, 2004, 3:33am UTC](https://forum.kirupa.com/t/dynamic-content-resorting-arrays/51335/1 "2004-05-12T03:33:36Z")

</div>

Ok, here’s what I’m trying to do. I must admit I’m a bit of a javascript novice though, I want to dynamically re-write a **portion** of a page (table contents) on the fly. Basically I want to be able to sort the contents of a table, and only re-write that particular part.

The document.write function appears to re-write the entire page. I can sort the contents fine, its the writing to the page I can’t figure out. Here’s the code I’ve written so far, I’d appreciate any javascript advice 😃

```php

<HTML>
<HEAD>
<TITLE> TEST HTML FOR JAVASCRIPT FUNCTIONS - ARRAY SORTING</TITLE>

<SCRIPT LANGUAGE="javascript">

myArray = new Array ("100", "10", "26", "600", "403")

function desc (a, b)
{
return b - a;
}

function asc (a, b)
{
return a - b;
}

</SCRIPT>
</HEAD>

<BODY>

<SCRIPT LANGUAGE="javascript">
function sort(what)
{
if (what == "up"){
myArray.sort(asc)
}
if (what == "down"){
myArray.sort(desc)
}
for (i=0; i < myArray.length; i++)
{
document.write("<TABLE WIDTH=500 border=1><TR>")
document.write("<TD>" + myArray* + "</TD>")
document.write("</TR></TABLE>")

}
}
sort('up')
</SCRIPT>

<INPUT type="button" name="ASC" value="ASC" onClick="sort('up')">
<INPUT type="button" name="DESC" value="DESC" onClick="sort('down')">
</BODY>
</HTML>

```
