/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] "Outline" sort possible?

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>
[/code]

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@mityaDec 12.2009 — [CODE] [COLOR="Red"]tarr[/COLOR] = tArr.sort();
document.getElementById('sortorder').innerHTML = 'Reg sort<p>'+[COLOR="#ff0000"]tArr[/COLOR].join('<br>');
[COLOR="#ff0000"]tarr[/COLOR] = tArr.sort(NumSort);
document.getElementById('numborder').innerHTML = 'Num sort<p>'+[COLOR="#ff0000"]tArr[/COLOR].join('<br>');[/CODE]


Possibly because you've got tarr where you probably wanted t[B]A[/B]rr?
Copy linkTweet thisAlerts:
@JMRKERauthorDec 12.2009 — Thanks for the thought.

Typo on my part, :o , but no different when fixed ...
<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Outline Sort&lt;/title&gt;
&lt;style type="text/css"&gt;
.box { width:100px; float:left; border:1px solid blue; }
&lt;/style&gt;
&lt;script type="text/javascript"&gt;
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',
'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&lt;p&gt;'+Arr.join('&lt;br&gt;');
var tArr = Arr.sort(randOrd);
document.getElementById('randorder').innerHTML = 'Random&lt;p&gt;'+tArr.join('&lt;br&gt;');
tArr = tArr.sort();
document.getElementById('sortorder').innerHTML = 'Reg sort&lt;p&gt;'+tArr.join('&lt;br&gt;');
tArr = tArr.sort(NumSort);
document.getElementById('numborder').innerHTML = 'Num sort&lt;p&gt;'+tArr.join('&lt;br&gt;');
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body onload="DoIt()"&gt;
&lt;div class="box" id="origorder"&gt;&lt;/div&gt;
&lt;div class="box" id="randorder"&gt;&lt;/div&gt;
&lt;div class="box" id="sortorder"&gt;&lt;/div&gt;
&lt;div class="box" id="numborder"&gt;&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@mrhooDec 12.2009 — You could

1. Sort a copy, not the original, with array.slice.

  • 2. Split each string on the dots and compare the pieces


  • [CODE]function sortdigitsanddots(A, save){
    if(save) A= A.slice(0);
    return A.sort(function(a, b){
    a= a.split('.');
    b= b.split('.');
    var a0, b0;
    while(a.length && b.length && a0== b0){
    a0= a.shift() || 0;
    b0= b.shift() || 0;
    }
    if(a0== b0) return a.length? 1: b.length? -1: 0;
    return a0-b0;

    })
    }[/CODE]


    var arr= ['3.1','20','1.1','10.2','10.3.2','3.3','10.1',

    '2.11','2','3.2','10','20.1.1','20.1','1.2.2',

    '10.3.1','3','20.1.2','1','2.12','1.2','1.2.1'];

    [B]var B= sortdigitsanddots(arr, true);[/B]

    // pass true to return a sorted copy, leave the original untouched

    alert('Original:n'+arr.join(', ')+'nnSorted:n'+B.join(', ') );


    /* returned value:

    [B]Original:[/B]

    3.1, 20, 1.1, 10.2, 10.3.2, 3.3, 10.1, 2.11, 2, 3.2, 10, 20.1.1, 20.1, 1.2.2, 10.3.1, 3, 20.1.2, 1, 2.12, 1.2, 1.2.1

    [B]Sorted:[/B]

    1, 1.1, 1.2, 1.2.1, 1.2.2, 2, 2.11, 2.12, 3, 3.1, 3.2, 3.3, 10, 10.1, 10.2, 10.3.1, 10.3.2, 20, 20.1, 20.1.1, 20.1.2

    */
    Copy linkTweet thisAlerts:
    @JMRKERauthorDec 12.2009 — Thank you 'mrhoo'.

    I'll give that a test in my real program and report back.

    ?
    Copy linkTweet thisAlerts:
    @JMRKERauthorDec 14.2009 — Thank you 'mrhoo'.

    Works perfectly in my application.

    ?
    ×

    Success!

    Help @JMRKER spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 4.29,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...