I’m trying to do what might be called an “Outline” sort but I’m having little success. 😮
In the following I create an outline numbered array of strings.
Then I randomize the contents and display the regular and numeric sort order.
Problem appears to be that either
1. the multiple ‘.’ in the outline elements cause the numeric sort to believe they are string.
OR
2. the string sort is ignoring the ‘.’ characters in determining the display order.
Any ideas on how to sort to the original order after it has been changed? ?
[code]
<html>
<head>
<title>Outline Sort</title>
<style type=”text/css”>
.box { width:100px; float:left; border:1px solid blue; }
</style>
<script type=”text/javascript”>
var Arr = [‘1′,’1.1’,
‘1.2’,’1.2.1′,’1.2.2′,
‘2’,’2.11′,’2.12′,
‘3’,’3.1′,’3.2′,’3.3′,
// etc.
’10’,’10.1′,’10.2′,’10.3.1′,’10.3.2′,
’20’,’20.1′,’20.1.1′,’20.1.2′];
function randOrd() { return (Math.round(Math.random())-0.5); }
function NumSort(a,b) { return a-b; }
function DoIt() {
document.getElementById(‘origorder’).innerHTML = ‘Orig<p>’+Arr.join(‘<br>’);
var tArr = Arr.sort(randOrd);
document.getElementById(‘randorder’).innerHTML = ‘Random<p>’+tArr.join(‘<br>’);
tarr = tArr.sort();
document.getElementById(‘sortorder’).innerHTML = ‘Reg sort<p>’+tArr.join(‘<br>’);
tarr = tArr.sort(NumSort);
document.getElementById(‘numborder’).innerHTML = ‘Num sort<p>’+tArr.join(‘<br>’);
}
</script>
</head>
<body onload=”DoIt()”>
<div class=”box” id=”origorder”></div>
<div class=”box” id=”randorder”></div>
<div class=”box” id=”sortorder”></div>
<div class=”box” id=”numborder”></div>
</body>
</html>