简体   繁体   English

class 的对象数组内的对象属性的排序方法

[英]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.我需要地址簿 class 中的 sort() 方法的帮助。 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.我试图从 stackoverflow 上的示例中自己弄清楚,但我似乎无法让它工作,因为大多数示例不涉及从 class 实例工作。 If you could please look at the sort() method and let me know where I am going wrong.如果可以,请查看 sort() 方法并让我知道我哪里出错了。 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您可以在Mozilla 开发人员处了解更多信息

I assume you want to sort this.contactList in-place.我假设您想对this.contactList进行就地排序。
Note that you do not perform any assignment to this.contactList in your sort() code.请注意,您没有在sort()代码中对this.contactList执行任何分配。 This is the first bug.这是第一个错误。
The second bug is that you return a value from the function immediately, instead of sorting your data.第二个错误是您立即从 function 返回一个值,而不是对数据进行排序。
The third bug is, that you cannot sort in O(N) complexity (ie with a single pass on the data).第三个错误是,您无法按O(N)复杂度进行排序(即单次传递数据)。

You need to decide which sorting algorithm you want to implement, or use the the native javascript implementation which is MergeSort .您需要决定要实现哪种排序算法,或者使用本机 javascript 实现,即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.在这种情况下,您需要传递一个 function 来表达您想要对数据进行排序的方式和使用哪些属性,这就是您尝试做的,使用 -1、1 和 0。

In this case, you can implement sort() in the following way:在这种情况下,您可以通过以下方式实现sort()

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();                            
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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