Suppoose I have HTML structure as below:
<div class="fullView" id="listParent" style="display: block;">
<div role="row" class="row entry" id="1">
<div class="col2 name" role="gridcell">
<div class="cell fileName" id="WC_RequisitionList_TableContent_name_1">
<a href="#" id="WC_RequisitionList_Link_2_1">b2cList</a>
</div>
</div>
</div>
<div role="row" class="row entry" id="2">
<div class="col2 name" role="gridcell">
<div class="cell fileName" id="WC_RequisitionList_TableContent_name_1">
<a href="#" id="WC_RequisitionList_Link_2_1">Order 10</a>
</div>
</div>
</div>
<div role="row" class="row entry" id="3">
<div class="col2 name" role="gridcell">
<div class="cell fileName" id="WC_RequisitionList_TableContent_name_1">
<a href="#" id="WC_RequisitionList_Link_2_1">Order 11</a>
</div>
</div>
</div>
<div role="row" class="row entry" id="4">
<div class="col2 name" role="gridcell">
<div class="cell fileName" id="WC_RequisitionList_TableContent_name_1">
<a href="#" id="WC_RequisitionList_Link_2_1">Order 10</a>
</div>
</div>
</div>
<div role="row" class="row entry" id="5">
<div class="col2 name" role="gridcell">
<div class="cell fileName" id="WC_RequisitionList_TableContent_name_1">
<a href="#" id="WC_RequisitionList_Link_2_1">12345</a>
</div>
</div>
</div>
<div role="row" class="row entry" id="6">
<div class="col2 name" role="gridcell">
<div class="cell fileName" id="WC_RequisitionList_TableContent_name_1">
<a href="#" id="WC_RequisitionList_Link_2_1">QO</a>
</div>
</div>
</div>
<div role="row" class="row entry" id="7">
<div class="col2 name" role="gridcell">
<div class="cell fileName" id="WC_RequisitionList_TableContent_name_1">
<a href="#" id="WC_RequisitionList_Link_2_1">1234</a>
</div>
</div>
</div>
<div role="row" class="row entry" id="8">
<div class="col2 name" role="gridcell">
<div class="cell fileName" id="WC_RequisitionList_TableContent_name_1">
<a href="#" id="WC_RequisitionList_Link_2_1">123456</a>
</div>
</div>
</div>
<div role="row" class="row entry" id="9">
<div class="col2 name" role="gridcell">
<div class="cell fileName" id="WC_RequisitionList_TableContent_name_1">
<a href="#" id="WC_RequisitionList_Link_2_1">Order10</a>
</div>
</div>
</div>
</div>
on load it is rendered as above i.e b2cList Order 10 Order 11 Order 10 12345 QO 1234 123456 Order10
Now, I want to sort in ascending order of the above structure as the below order: b2cList Order 10 Order 10 Order10 Order 11 QO 1234 12345 123456
The above HTML structure is one of column of a table like structure (here i am not using '<table>
' tag). Now I want to sort the above column based on the text as mentioned above on a selecting an option from dropdown. I am using bubble sort.
Here is code I have written so far.
var a = document.getElementById("listParent");
for ( var i = 0; i < a.childNodes.length; i++) {
var swapped = false;
for ( var j = 1; j < a.childNodes.length; j++) {
var divId = document.getElementById('WC_orderSearchResultsArea_Link_2_' + (j - 1));
if (divId) {
var listName = divId.innerHTML.toLowerCase();
if (listName) {
var innerDivId = document.getElementById('WC_orderSearchResultsArea_Link_2_' + j);
if (innerDivId) {
var innerId = innerDivId.innerHTML.toLowerCase();
var firstNum = Number(listName);
var secondNum = Number(innerId);
if(Number.isNaN(firstNum) && Number.isNaN(secondNum)) {
if (listName > innerId) {
swapped = true;
swap(j - 1, j);
}
}
if(firstNum > secondNum) {
swapped = true;
swap(j - 1, j);
}
}
}
}
}
if (swapped == false) {
break;
}
}
swap = function(firstRowIndex, secondRowIndex) {
var firstRow = document.getElementById(firstRowIndex);
var secondRow = document.getElementById(secondRowIndex);
if (firstRow && secondRow) {
var tmp = firstRow.innerHTML;
firstRow.innerHTML = secondRow.innerHTML.replace(/_\d+\"/g, "_" + firstRowIndex + "\"");
secondRow.innerHTML = tmp.replace(/_\d+\"/g, "_" + secondRowIndex + "\"");
}
}
Please help.