Skip to main content

BackboneJs

28th October, 2022

Updated: 28th October, 2022

    Backbone/Underscore: Sort objects in ASC/DESC order by property

      this.renderFeedback(model);
    
                    var $userScored = model.get('score'); //find out what the user scored from the model
                    var sortable = [];
                    $(model.collection.models).each(function(i) {
                        if(this.get('type') == 'feedback') {
                            sortable.push(this); // put all the feedbacks into an array to be sorted. (just in case XML is out of order)
                        }
                    });
                    sortable.sort(function(a, b) {return a[1] - b[1]}); // Sort the array into ASC order
                    sortable.reverse(); // Reverse the array so the first item is the highest score
                    var $topScore = sortable[0].get('score'); // We're assuming here that you're wrong unless you scored the highest possible value
    
    //                console.log('user scored '+$userScored);
    //                console.log('top score '+$topScore);
    
                    if( $userScored < $topScore) // if the user scored less than the top marks, reattempt.
                    {
                        this.renderRetryButton();
                    }

    ee712946-a657-411d-8b9c-0f118a65d092

    Created on: 28th October, 2022

    Last updated: 28th October, 2022

    Tagged With: