dcsimg
www.webdeveloper.com
Results 1 to 2 of 2

Thread: Help with returning an array of records into descending order.

  1. #1
    Join Date
    Nov 2015
    Posts
    1

    Help with returning an array of records into descending order.

    Hello I'm stuck with this piece of code which is supposed to return an array of records in the style of urlScores(web,pattern) sorted into descending order of scores. Using the function rankedScores(web,pattern).
    Records with a score of zero are omitted. Currently my code for urlScores(web,pattern) seems to be working but rankedScores(web,pattern) isn't. Any advice and help would be great! Thank you

    Code:
    var contents = [ "Surrey University offers a range of degree programmes.", "An alternative University.", "A website for stuff."] 
    var pages = [ "|www.surrey.ac.uk|Surrey University offers a range of degree programmes." , "!www.abc.ac.uk!An alternative University" , "%www.xyz.co.uk%A website for stuff." ]
    var web = [ {url : "www.surrey.ac.uk",  content : "Surrey University offers a range of degree programmes." } , {url : "www.abc.ac.uk",  content : "An alternative University" } , {url : "www.xyz.co.uk",  content : "A website for stuff." } ]
    
    
    function urlScores(web, pattern) 
    {
    var records = {}; 
    var results = []; 
    var n = 0; 
    for(var i = 0; i < web.length; i++) 
    {
    n = score(web[i].content, pattern); 
    if(n != 0) 
    {
    records.url = web[i].url; 
    records.score = n; 
    results[results.length] = {url: records.url, score: records.score}; 
    }
    }
    return(results); 
    }
    
    
    function rankedScores(web, pattern) { 
    var v = urlScores(web, pattern); 
    var sorted = []; 
    var maxIndex = 0; 
    while (sorted.length < v.length) { 
    for (var i = 0; i < v.length; i += 1) {
    if (v[i].score > v[maxIndex].score) {
    maxIndex = i
    }
    }
    var clone = {};
    for(var i in v[maxIndex])
    clone[i] = v[maxIndex][i];
    sorted[sorted.length] = clone;
    v[maxIndex].score = 0;
    }
    alert(sorted[0].url + ' ' + sorted[0].score)
    alert(sorted[1].url + ' ' + sorted[1].score)
    alert(sorted[2].url + ' ' + sorted[2].score)
    }

  2. #2
    Join Date
    Dec 2012
    Posts
    893
    Indenting/beautifying your code increases readability:
    Code:
    function urlScores(web, pattern) {
        var records = {};
        var results = [];
        var n = 0;
        for (var i = 0; i < web.length; i++) {
            n = score(web[i].content, pattern);
            if (n != 0) {
                records.url = web[i].url;
                records.score = n;
                results[results.length] = {
                    url: records.url,
                    score: records.score
                };
            }
        }
        return (results);
    }
    
    
    function rankedScores(web, pattern) {
        var v = urlScores(web, pattern);
        var sorted = [];
        var maxIndex = 0;
        while (sorted.length < v.length) {
            for (var i = 0; i < v.length; i += 1) {
                if (v[i].score > v[maxIndex].score) {
                    maxIndex = i
                }
            }
            var clone = {};
            for (var i in v[maxIndex])
                clone[i] = v[maxIndex][i];
            sorted[sorted.length] = clone;
            v[maxIndex].score = 0;
        }
        alert(sorted[0].url + ' ' + sorted[0].score)
        alert(sorted[1].url + ' ' + sorted[1].score)
        alert(sorted[2].url + ' ' + sorted[2].score)
    }
    Why not use the built in sorting function of javascript?
    Code:
    v.sort(function(a, b) {return b.score - a.score};
    Last edited by Sempervivum; 11-06-2015 at 06:35 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles