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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Table sort - descending order

Status
Not open for further replies.

MoshiachNow

IS-IT--Management
Feb 6, 2002
1,851
IL
HI,

I have found the following code that nicely sorts my table columns in ascending order.
How do I convert it to have a descending order?
Thanks

<SCRIPT LANGUAGE="JavaScript">
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! -->
<!-- Begin
function setDataType(cValue)
{
// THIS FUNCTION CONVERTS DATES AND NUMBERS FOR PROPER ARRAY
// SORTING WHEN IN THE SORT FUNCTION
var isDate = new Date(cValue);
if (isDate == "NaN")
{
if (isNaN(cValue))
{
// THE VALUE IS A STRING, MAKE ALL CHARACTERS IN
// STRING UPPER CASE TO ASSURE PROPER A-Z SORT
cValue = cValue.toUpperCase();
return cValue;
}
else
{
// VALUE IS A NUMBER, TO PREVENT STRING SORTING OF A NUMBER
// ADD AN ADDITIONAL DIGIT THAT IS THE + TO THE LENGTH OF
// THE NUMBER WHEN IT IS A STRING
var myNum;
myNum = String.fromCharCode(48 + cValue.length) + cValue;
return myNum;
}
}
else
{
// VALUE TO SORT IS A DATE, REMOVE ALL OF THE PUNCTUATION AND
// AND RETURN THE STRING NUMBER
//BUG - STRING AND NOT NUMERICAL SORT .....
// ( 1 - 10 - 11 - 2 - 3 - 4 - 41 - 5 etc.)
var myDate = new String();
myDate = isDate.getFullYear() + " " ;
myDate = myDate + isDate.getMonth() + " ";
myDate = myDate + isDate.getDate(); + " ";
myDate = myDate + isDate.getHours(); + " ";
myDate = myDate + isDate.getMinutes(); + " ";
myDate = myDate + isDate.getSeconds();
//myDate = String.fromCharCode(48 + myDate.length) + myDate;
return myDate ;
}
}
function sortTable(col, tableToSort)
{
var iCurCell = col + tableToSort.cols;
var totalRows = tableToSort.rows.length;
var bSort = 0;
var colArray = new Array();
var oldIndex = new Array();
var indexArray = new Array();
var bArray = new Array();
var newRow;
var newCell;
var i;
var c;
var j;
// ** POPULATE THE ARRAY colArray WITH CONTENTS OF THE COLUMN SELECTED
for (i=1; i < tableToSort.rows.length; i++)
{
colArray[i - 1] = setDataType(tableToSort.cells(iCurCell).innerText);
iCurCell = iCurCell + tableToSort.cols;
}
// ** COPY ARRAY FOR COMPARISON AFTER SORT
for (i=0; i < colArray.length; i++)
{
bArray = colArray;
}
// ** SORT THE COLUMN ITEMS
//alert ( colArray );
colArray.sort();
//alert ( colArray );
for (i=0; i < colArray.length; i++)
{ // LOOP THROUGH THE NEW SORTED ARRAY
indexArray = (i+1);
for(j=0; j < bArray.length; j++)
{ // LOOP THROUGH THE OLD ARRAY
if (colArray == bArray[j])
{ // WHEN THE ITEM IN THE OLD AND NEW MATCH, PLACE THE
// CURRENT ROW NUMBER IN THE PROPER POSITION IN THE
// NEW ORDER ARRAY SO ROWS CAN BE MOVED ....
// MAKE SURE CURRENT ROW NUMBER IS NOT ALREADY IN THE
// NEW ORDER ARRAY
for (c=0; c<i; c++)
{
if ( oldIndex[c] == (j+1) )
{
bSort = 1;
}
}
if (bSort == 0)
{
oldIndex = (j+1);
}
bSort = 0;
}
}
}
// ** SORTING COMPLETE, ADD NEW ROWS TO BASE OF TABLE ....
for (i=0; i<oldIndex.length; i++)
{
newRow = tableToSort.insertRow();
for (c=0; c<tableToSort.cols; c++)
{
newCell = newRow.insertCell();
newCell.innerHTML = tableToSort.rows(oldIndex).cells(c).innerHTML;
}
}
//MOVE NEW ROWS TO TOP OF TABLE ....
for (i=1; i<totalRows; i++)
{
tableToSort.moveRow((tableToSort.rows.length -1),1);
}
//DELETE THE OLD ROWS FROM THE BOTTOM OF THE TABLE ....
for (i=1; i<totalRows; i++)
{
tableToSort.deleteRow();
}
}
// End -->
</script>

###############################
my Table:
###############################

<table name="rsTable" id=rsTable cols=9 border="1" width="100%" cellspacing="0" cellpadding="0" style="font-size: 14px;">
<TR bgcolor=mediumblue>
<TD><A href="javascript:sortTable(0, rsTable);"><FONT color=white><B>PID </FONT></B></A></TD>
<TD><A href="javascript:sortTable(1, rsTable);"><FONT color=white><B>PrntPID</FONT></B></A></TD>
<TD><A href="javascript:sortTable(2, rsTable);"><FONT color=white><B>Name</FONT></B></A></TD>
<TD><A href="javascript:sortTable(3, rsTable);"><FONT color=white><B>ThrdCNT</FONT></B></A></TD>
<TD><A href="javascript:sortTable(4, rsTable);"><FONT color=white><B>HndlCNT</FONT></B></A></TD>
<TD><A href="javascript:sortTable(5, rsTable);"><FONT color=white><B>WrkngSize</FONT></B></A></TD>
<TD><A href="javascript:sortTable(6, rsTable);"><FONT color=white><B>PkWrkngSize</FONT></B></A></TD>
<TD><A href="javascript:sortTable(7, rsTable);"><FONT color=white><B>VirtualSize</FONT></B></A></TD>
<TD><A href="javascript:sortTable(8, rsTable);"><FONT color=white><B>CreationDate</FONT></B></A></TD>
</TR></FONT>
<tr>
<td width="10%">1192</td>
<td width="10%">804</td>
<td width="10%">svchost.exe</td>
<td width="10%">76</td>
<td width="10%">2189</td>
<td width="10%">62427136</td>
<td width="10%">162353152</td>
<td width="10%">242917376</td>
<td width="10%">02.18 08:09:33</td>
</tr>
<tr>
<td width="10%">1996</td>
<td width="10%">804</td>
<td width="10%">Mcshield.exe</td>
<td width="10%">21</td>
<td width="10%">206</td>
<td width="10%">39178240</td>
<td width="10%">50528256</td>
<td width="10%">97116160</td>
<td width="10%">02.18 08:09:40</td>
</tr>
<tr>
<td width="10%">1844</td>
<td width="10%">804</td>
<td width="10%">inetinfo.exe</td>
<td width="10%">16</td>
<td width="10%">302</td>
<td width="10%">34447360</td>
<td width="10%">34664448</td>
<td width="10%">45735936</td>
<td width="10%">02.18 08:09:37</td>
</tr>
</table>

Long live king Moshiach !
 
There is no "exact change". You have to write the "exact" code to sort in descending order. You can use the ascending order sort as a template, but you have to think for yourself how to sort in the opposite direction.

Lee
 
One has to sort out exactly the meaning of ascending and descending. There one can customize it: that's the point of the spec. For ascii string, for instance, one meaning of "order" can be done like this. (It is by no mean a unique way to define "order".)

[1] Add two functions to the script sector, one is meant for ascending, another descending.
[tt]
function sortAscAscii(a,b) {
for (var i=0;i<a.length;i++) {
if ((a.charCodeAt(i)>b.charCodeAt(i)) || (i>b.length-1)) {
return 1; //a after b
} else if (a.charCodeAt(i)<b.charCodeAt(i)) {
return -1; //a before b
}
}
return 0; //a and b at par, all characters are equal
}

function sortDesAscii(a,b) {
for (var i=0;i<a.length;i++) {
if ((a.charCodeAt(i)<b.charCodeAt(i))) {
return 1; //a after b
} else if ((a.charCodeAt(i)>b.charCodeAt(i)) || (i>b.length-1)) {
return -1; //a before b
}
}
return 0; //a and b at par, all characters are equal
}
[/tt]
[2] Then change the line to reflect the desired order (of descending, I suppose.) If it is meant again for ascending (default), just change the reference to sortAscAscii function name.

>colArray.sort();
[tt]colArray.sort([blue]sortDesAscii[/blue]);[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top