Link to home
Start Free TrialLog in
Avatar of mildurait
milduraitFlag for Australia

asked on

Sorting Javascript Array using the QuickSort Algorithm

I have the following javascript code which sorts <tr> elements within a <tbody> container.
As I have a large number of <tr> elements to sort, I am wondering if the quick sort method would be appopriate, if anyone can translate the code I have.
The wikipedia reference to Quicksort is here http://en.wikipedia.org/wiki/Quicksort

var trTagsArray = new Array();
 
function initTBody(tdBodyID){
  var tbodytag = document.getElementById(tdBodyID);
  var tbodytagChilds = tbodytag.childNodes
  var i = 0;
  for (i=0;i<tbodytagChilds.length;i++){
    if (tbodytagChilds[i].nodeName == "TR"){
      trTagsArray.push(tbodytagChilds[i]);
    }
  }
}
 
function reOrder(tbodyid,o1,o2,o3){
  var tbodytag = document.getElementById(tbodyid);
  var tbodytagChilds = tbodytag.childNodes;
  var splicedTrTag;
  var oValue;
  var i = 0;
  var smallestSortValue = "ZZZZZZZZZZ";
  var smallestIndex;
  var tempArray = new Array().concat(trTagsArray);
  var tempArray2 = new Array();
  var sortValue="";
  while (tbodytagChilds.length > 0){
    tbodytag.removeChild(tbodytagChilds[0]);
  }
    for (var k=3;k>=1;k--){
    oValue = eval("o" + k);
    while (tempArray.length > 0){
      smallestSortValue = "ZZZZZZZZZZ";
      for (i=0;i<tempArray.length;i++){
        sortValue = getSortValue(tempArray[i],oValue);
        if (sortValue < smallestSortValue){
          smallestSortValue = sortValue;
          smallestIndex = i;
        }
      }
      splicedTrTag = tempArray[smallestIndex];
      tbodytag.appendChild(splicedTrTag);
      tempArray.splice(smallestIndex,1);
      tempArray2.push(splicedTrTag);
    }  
    
    tempArray = new Array().concat(tempArray2);
    tempArray2 = new Array();
  }
 
}
 
function getSortValue(trTag,tagIndex){
  var trTagChilds = trTag.childNodes;
  var indexedTdTag;
  var numTdTag = 0;
  var j=0;
  var returnValue = "";
  for (j=0;j<trTagChilds.length;j++){
    if (trTagChilds[j].nodeName == "TD"){
      ++numTdTag;
      if (tagIndex == numTdTag){
        indexedTdTag = trTagChilds[j];
        break;
      }
    }
  }
  
  try{
    returnValue = indexedTdTag.attributes['sortvalue'].value;
  }catch(err){
    returnValue = "";
  }
  
  return returnValue;
}

Open in new window

Avatar of Kin Fat SZE
Kin Fat SZE
Flag of Hong Kong image

mildurait,
I have tried to solve it out...
But... I facing several problems
1) pass arrays to the function Quicksort Recursively
2) threading problem, I can't wait until first job was finished then fire another one ....
Sorry, I am not Jesus !

<table>
     <tr>
          <td onclick="reOrder('myTBody',1,0,0);">ID</td>
          <td onclick="reOrder('myTBody',2,3,1);">Name</td>
          <td onclick="reOrder('myTBody',3,2,1);">DOB</td></tr>
     </tr>
     <tbody id="myTBody">
         <tr ordernum=1>
            <td sortvalue="0000001">1</td>
            <td sortvalue="Peter">Peter</td>
            <td sortvalue="19750119">19/01/1975</td>
          </tr>
            <tr ordernum=3>
                <td sortvalue="0000004">4</td>
                <td sortvalue="Jane">Jane</td>
                <td sortvalue="19800201">01/02/1980</td>
            </tr>
           <tr ordernum=2>
              <td sortvalue="0000002">2</td>
              <td sortvalue="Mark">Mark</td>
              <td sortvalue="19880731">31/07/1988</td>
           </tr>
            <tr ordernum=3>
                <td sortvalue="0000003">3</td>
                <td sortvalue="Jane">Jane</td>
                <td sortvalue="19600201">01/02/1960</td>
            </tr>
           <tr ordernum=4>
              <td sortvalue="0000002">2</td>
              <td sortvalue="Mark">Mark</td>
              <td sortvalue="19890731">31/07/1989</td>
           </tr>
            <tr ordernum=5>
                <td sortvalue="0000003">3</td>
                <td sortvalue="Jane">Jane</td>
                <td sortvalue="20000201">01/02/2000</td>
            </tr>
     </tbody>
</table>
 
<script type="text/javascript">
var trTagsArray = new Array();
 
function initTBody(){
  var tbodytag = document.getElementById("myTBody");
  var tbodytagChilds = tbodytag.childNodes
  var i = 0;
  for (i=0;i<tbodytagChilds.length;i++){
    if (tbodytagChilds[i].nodeName == "TR"){
      trTagsArray.push(tbodytagChilds[i]);
    }
  }
}
 
 
function reOrder(tbodyid,o1,o2,o3){
  var tbodytag = document.getElementById(tbodyid);
  var tbodytagChilds = tbodytag.childNodes;
  var splicedTrTag;
  var oValue;
  var i = 0;
  var smallestSortValue = "ZZZZZZZZZZ";
  var smallestIndex;
  var tempArray = new Array().concat(trTagsArray);
  var tempArray2 = new Array();
  var sortValue="";
  while (tbodytagChilds.length > 0){
    tbodytag.removeChild(tbodytagChilds[0]);
  }
 
 
  for (var k=3;k>=1;k--){
    oValue = eval("o" + k);
//    alert("oValue : " + oValue);
    tempArray = Quicksort(tempArray,oValue);
  }
 
 
//  tempArray = Quicksort(tempArray,3);
 
    for (i = 0; i< tempArray.length;i++){
        tbodytag.appendChild(tempArray[i]);
    }
}
 
function Quicksort(sortArray,voValue){
 
      var tempArray = new Array().concat(sortArray);
      var lessArray = new Array();
      var moreArray = new Array();
      var resultArray = new Array();
      var sortValue="";
 
      if (sortArray.length == 1){
        return sortArray[0];
      }else{
 
      pivot = tempArray.pop();
      pivotValue = getSortValue(pivot, voValue);
 
      for (i=0;i<tempArray.length;i++){
        if (voValue ==3){
          sortValue = parseInt(getSortValue(tempArray[i],voValue));
        }else{
                sortValue = getSortValue(tempArray[i],voValue);
        }
        if (sortValue <= pivotValue){
          lessArray.push(tempArray[i]);
        }else{
          moreArray.push(tempArray[i]);
        }
      }
 
      
      
      if (lessArray.length > 0){
        resultArray = resultArray.concat(Quicksort(new Array(lessArray),voValue));
      }
 
      resultArray = resultArray.concat(pivot);
 
 
      if (moreArray.length > 0){
        resultArray = resultArray.concat(Quicksort(new Array(moreArray),voValue));
      }
 
      return resultArray;
      }
 
}
 
function getSortValue(trTag,tagIndex){
  var trTagChilds = trTag.childNodes;
  var indexedTdTag;
  var numTdTag = 0;
  var j=0;
  var returnValue = "";
  for (j=0;j<trTagChilds.length;j++){
    if (trTagChilds[j].nodeName == "TD"){
      ++numTdTag;
      if (tagIndex == numTdTag){
        indexedTdTag = trTagChilds[j];
        break;
      }
    }
  }
  
  try{
    returnValue = indexedTdTag.attributes['sortvalue'].value;
  }catch(err){
    returnValue = "";
  }
  
  return returnValue;
}
 
initTBody();
</script>

Open in new window

Avatar of mildurait

ASKER

@fsze88
So if I hear you right
a) this is a burden which is way too heavy and impossible for you to carry alone?
b) only Jesus, the living Son of God, who has the authority over all Heaven and Earth would have the ability to solve the insolvable?
mildurait,
I will keep try to solve this.... give me times. Also, I interested in this question but not guarantee I can/able to solve it out....
@fsze88
You've done great things for me so far, and I appreciate your help.
We might need to develop a replacement language for JavaScript, called JesusScript which would by definition support an unlimited number of concurrent recursive calls.
ASKER CERTIFIED SOLUTION
Avatar of Kin Fat SZE
Kin Fat SZE
Flag of Hong Kong image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
did you tried it? It works on QuickSort Algorithm
Well done again!
Thanks for your time and expertise.