Tell me more ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have this javascript code and im not happy the way it is written..

function notesViewModel() {
    _this.colName = "CreatedDate";
    _this.sortOrder = "desc";
    _this.notes = ko.observableArray();

   _this.SortItems = function (colname) {
        var newNotes = _this.notes();
        if (_this.sortOrder === "desc") {
            this.notes(newNotes.sort(notesViewModel._getSortFunction = function (a, b) {
                return a[colname] < b[colname] ? -1 : 1;
            }));
            switchSortOrder();
        } else {
            this.notes(newNotes.sort(notesViewModel._getSortFunction = function (a, b) {
                return a[colname] > b[colname] ? -1 : 1;
            }));
            switchSortOrder();
        }
    };
    function switchSortOrder() {
        if (_this.sortOrder === "asc") {
            _this.sortOrder = "desc";
        } else {
            _this.sortOrder = "asc";
        }
    }
 ko.applyBindings(_this, $("body").get(0));
return _this;
}

how can I pass the sort order into anonymous function and call it to return the required bubble sort?

share|improve this question
What is _this? It's not this... – Izkata Apr 28 at 23:37

closed as off topic by World Engineer Apr 29 at 1:29

Questions on Programmers Stack Exchange are expected to relate to software development within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

Browse other questions tagged or ask your own question.