javascript - how to create an array with 3 students and have them sorted by firstname and nid -


i need create object "student" 3 properties: first name, last name, nid (5-digit number).

create array "myclass" 3 students.

sort myclass firstname.

sort myclass nid.

i have of set don't know arrays , objects. need doing proper way , explained

var student = (firstname, lastname, nid) {    this.firstname = firstname;    this.lastname = lastname;    this.nid = nid;    this.myclass = myclass;  }    var myclass = {    "myclass": {      student.firstname: "matthew",      student.lastname: "stone",      student.nid: 458293    },      "myclass": {      student.firstname: "arian",      student.lastname: "minaie",      student.nid: 891023    },      "myclass": {      student.firstname: "pelin",      student.lastname: "schvechsky",      student.nid: 496054    }    }    function myfunction1() {    myclass.sort();    document.getelementbyid("post").innerhtml = firstname;  }    function myfunction2() {    myclass.sort();    document.getelementbyid("post").innerhtml = nid;  }
<p id="post"></p>    <button onclick="myfunction1()">sort alphabetically first name!</button>  <button onclick="myfunction2()">sort numerically nid!</button>

you use constructor student , create new instances students. need sort array wanted properties.

function student(firstname, lastname, nid) {      this.firstname = firstname;      this.lastname = lastname;      this.nid = nid;  }    function sortbyfirstnameasc() {      myclass.sort(function (a, b) {          return a.firstname.localecompare(b.firstname);      });      document.getelementbyid("post").innerhtml = json.stringify(myclass, 0, 4);  }    function sortbynidasc() {      myclass.sort(function (a, b) {          return a.nid - b.nid;      });      document.getelementbyid("post").innerhtml = json.stringify(myclass, 0, 4);  }    var myclass = [          new student("matthew", "stone", 458293),          new student("arian", "minaie", 891023),          new student("pelin", "schvechsky", 496054)      ];
<button onclick="sortbyfirstnameasc()">sort alphabetically first name!</button>  <button onclick="sortbynidasc()">sort numerically nid!</button>  <pre id="post"></pre>


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -