JHillman
JHillman

Reputation: 29

Order an array issues

Okay, so I created a gradebook of sorts. You add in your first and last name, and the grade, and it adds it to the table (It works when I run on Chrome, but not on jsfiddle for some reason. That's not the issue)

https://jsfiddle.net/bhLhayar/

The problem I have is with the sorting. My radio buttons say "not defined" but I thought I had them defined in my JS. Advice please? XD

function sortby(what){
if(what == 'firstN'){studentdata.sortbyfirstSet();}
if(what == 'lastN'){studentdata.sortbylastSet();}
if(what == 'gradeUp'){studentdata.sortbygradeSetUp();}
if(what == 'gradeDown'){studentdata.sortbygradeSetDown();}
updatetable()
}

Here is the code where I think I went wrong?. Has to do with this:

sortbyFirstSet(){
        this.data.sort(function (one, two){
            if(one.firstName > two.firstName){return 1}
            if(one.firstName < two.firstName){return -1}
            if(one.firstName == two.firstName){return 0}
        });
    }
    sortbylastSet(){
        this.data.sort(function (one, two){
            if(one.lastName > two.lastName){return 1}
            if(one.lastName < two.lastName){return -1}
            if(one.lastName == two.lastName){return 0}
        });
    }
    sortbygradeSetUp(){
        this.data.sort(function (one, two){
            if(one.finalGrade > two.finalGrade){return 1}
            if(one.finalGrade < two.finalGrade){return -1}
            if(one.finalGrade == two.finalGrade){return 0}
        });
    }
  sortbygradeSetDown(){
    this.data.sort(function (one, two){
      if(one.finalGrade < two.finalGrade){return 1}
      if(one.finalGrade > two.finalGrade){return -1}
      if(one.finalGrade == two.finalGrade){return 0}
    });
  }

Upvotes: 0

Views: 73

Answers (2)

H77
H77

Reputation: 5967

You seem to be passing in a string as a variable. Try onclick="sortby('lastN')" etc. instead.

And you don't have an updatetable function. Perhaps you wanted to call updateGradebook()?

Update One more thing, your first call to the sort method is misspelled. Should be sortbyFirstSet

Upvotes: 1

Hector Barbossa
Hector Barbossa

Reputation: 5528

Please correct your onclick code on radio buttons to sortby('firstN') from sortby(firstN). Also it seems that studentdata is an array and and you are trying to call the sortby functions using that.

Upvotes: 0

Related Questions