简体   繁体   中英

Sort method for an objects property that is inside an array of objects from a class

I need help with my sort() method from inside the AddressBook class. I tried to figure it out on my own from examples on stackoverflow but I can't seem to get it to work since most of the examples don't involve working from a class instance. If you could please look at the sort() method and let me know where I am going wrong. I think i need to loop through somehow and then reposition the array order.

window.onload = init;

let abm;

function init() {

abm = new AddressBook();

}

class Contact {
  constructor(name, email) {
  this.name = name;
  this.email = email;
  }
}

//DO NOT MODIFY ABOVE THIS LINE

function formSubmitted() {

    event.preventDefault();
    var user = document.getElementById("name").value;
    var mail = document.getElementById("email").value;

    var newContact = new Contact(user, mail);

    abm.add(newContact);
    abm.display();
}

function sortList() {
//CODE HERE ONLY
    abm.sort();
    abm.display();
}

class AddressBook {
 constructor() {
  this.contactList = [];
}

add(contact) {
//CODE HERE ONLY
    this.contactList.push(contact);             
}

 display(htmlId) {
//CODE HERE ONLY
        var html = "<table border='1|1'>";          

        for (var i = 0; i < this.contactList.length; i++){
            html+="<tr>";
            html+="<td>"+this.contactList[i].name+"</td>";
            html+="<td>"+this.contactList[i].email+"</td>";
            html+="</tr>";  
        }

        html+="</table>";

        document.getElementById("contacts").innerHTML = html;

 }
 sort() {
//CODE HERE ONLY
  for (var i = 0; i < this.contactList.length; i++){

        var tA = this.contactList[i].name.toUpperCase();
        var tB = this.contactList[i].name.toUpperCase();
        if (tA < tB) {
            return -1;
        }
        if (tA > tB) {
        return 1;
        }
        return 0;                               
        }
  }  
}
this.contactList.sort((a, b) => a.name.toUpperCase() - b.name.toUpperCase());

You can learn more at Mozilla Developers

I assume you want to sort this.contactList in-place.
Note that you do not perform any assignment to this.contactList in your sort() code. This is the first bug.
The second bug is that you return a value from the function immediately, instead of sorting your data.
The third bug is, that you cannot sort in O(N) complexity (ie with a single pass on the data).

You need to decide which sorting algorithm you want to implement, or use the the native javascript implementation which is MergeSort .
In this case, you'd need to pass a function to express how and using which properties you want to sort your data, which is kind of what you tried to do, using -1, 1, and 0.

In this case, you can implement sort() in the following way:

sort() {
    this.contactList = this.contactList.sort(function(a, b) {
        var tA = this.contactList[i].name.toUpperCase();
        var tB = this.contactList[i].name.toUpperCase();
        if (tA < tB) {
            return -1;
        }
        else if (tA > tB) {
            return 1;
        }

        return 0;                               
    }
}

Or in an equivalent way (make sure you understand why it's equivalent):

sort() {
    this.contactList = this.contactList.sort(function(a, b) {
        return a.name.toUpperCase() - b.name.toUpperCase();                            
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM