C# 淘汰排序不';我好像什么也没做

C# 淘汰排序不';我好像什么也没做,c#,javascript,knockout.js,C#,Javascript,Knockout.js,我的设置如下 我有一个服务,它将数据从存储过程返回到UI,没有特定的顺序。UI使用敲除将数据绑定到MVC部分,如下所示 @{ ViewBag.Title = "People List"; } <h2>People List</h2> <table id="people"> <thead> <tr> <th>Name</th> <th

我的设置如下

我有一个服务,它将数据从存储过程返回到UI,没有特定的顺序。UI使用敲除将数据绑定到MVC部分,如下所示

@{ ViewBag.Title = "People List"; }

<h2>People List</h2>

<table id="people">
    <thead>
        <tr>
            <th>Name</th>
            <th>Gender</th>
            <th>Job Title</th>
            <th>Job Description</th>
        </tr>
    </thead>
    <tbody data-bind="foreach: people">
        <tr>
            <td style="width: 125px;"><span data-bind="text: Name"></span></td>
            <td style="width: 75px;"><span data-bind="text: Gender"></span></td>
            <td style="width: 105px;"><span data-bind="text: JobTitle"></span></td>
            <td style="width: 300px;"><span data-bind="text: JobDescription"></span></td>
        </tr>
    </tbody>
</table>
<button style="margin-top: 15px;" data-bind="click: sortByName">Sort by Name</button>
<script type="text/javascript">
    function Person(data) {
        this.Name = ko.observable(data.Name);
        this.Gender = ko.observable(data.Gender);
        this.JobTitle = ko.observable(data.JobTitle);
        this.JobDescription = ko.observable(data.JobDescription);
    }

    function PersonListVM() {
        var self = this;

        self.people = ko.observableArray([]);

        $.getJSON('@Url.Action("DisplayPeople", "People")',
            function (results) {
                for (var i = 0; i < results.length; i++) {
                    self.people.push(new Person(results[i]));
                }
            });

        self.sortByName = function () {
            self.people.sort(function (x, y) {
                return x["Name"] == y["Name"] ? 0 : (x["Name"] < y["Name"] ? -1 : 1);
            });
        };
    };

    ko.applyBindings(new PersonListVM());
</script>
@{ViewBag.Title=“人员列表”}
人员名单
名称
性别
职位名称
职位描述
按名称排序
虚拟机的JS看起来像这样

@{ ViewBag.Title = "People List"; }

<h2>People List</h2>

<table id="people">
    <thead>
        <tr>
            <th>Name</th>
            <th>Gender</th>
            <th>Job Title</th>
            <th>Job Description</th>
        </tr>
    </thead>
    <tbody data-bind="foreach: people">
        <tr>
            <td style="width: 125px;"><span data-bind="text: Name"></span></td>
            <td style="width: 75px;"><span data-bind="text: Gender"></span></td>
            <td style="width: 105px;"><span data-bind="text: JobTitle"></span></td>
            <td style="width: 300px;"><span data-bind="text: JobDescription"></span></td>
        </tr>
    </tbody>
</table>
<button style="margin-top: 15px;" data-bind="click: sortByName">Sort by Name</button>
<script type="text/javascript">
    function Person(data) {
        this.Name = ko.observable(data.Name);
        this.Gender = ko.observable(data.Gender);
        this.JobTitle = ko.observable(data.JobTitle);
        this.JobDescription = ko.observable(data.JobDescription);
    }

    function PersonListVM() {
        var self = this;

        self.people = ko.observableArray([]);

        $.getJSON('@Url.Action("DisplayPeople", "People")',
            function (results) {
                for (var i = 0; i < results.length; i++) {
                    self.people.push(new Person(results[i]));
                }
            });

        self.sortByName = function () {
            self.people.sort(function (x, y) {
                return x["Name"] == y["Name"] ? 0 : (x["Name"] < y["Name"] ? -1 : 1);
            });
        };
    };

    ko.applyBindings(new PersonListVM());
</script>

职能人员(数据){
this.Name=ko.observable(data.Name);
这个。性别=可观察(数据。性别);
this.JobTitle=ko.可观察(data.JobTitle);
this.jobscription=ko.可观察(data.jobscription);
}
函数PersonListVM(){
var self=这个;
self.people=ko.observearray([]);
$.getJSON('@Url.Action(“DisplayPeople”,“People”),
职能(结果){
对于(var i=0;i
我可以毫无问题地获取和显示数据,甚至在VM函数中添加另一个虚拟人也可以顺利完成。但按名称排序似乎毫无用处。没有错误,但我显然做错了什么。这应该是一个非常简单的搜索,我遗漏了什么


PS:我将属性名称作为字符串提供的原因是,我希望在此表中生成一个可排序、分页的网格,并按表头排序。我的目标是能够传入任何有效的属性,在这些属性上表可以动态排序,但这只是一个实验。

就在我脑海中,你是否尝试过使用点语法而不是数组语法来访问person对象属性

self.people.sort(function (x, y) {
            return x.Name == y.Name ? 0 : (x.Name < y.Name ? -1 : 1);
        });
self.people.sort(函数x,y){
返回x.Name==y.Name?0:(x.Name
即使您使用数组语法访问属性,它们仍然是可观察的。您需要“展开”可观察对象以获取值

请尝试以下方法:

return x['Name']() == y['Name']() ? 0 : (x['Name']() < y['Name']() ? -1 : 1);
返回x['Name']()==y['Name']()?0:(x['Name']()
啊,是的,我想你是对的,我没有看到他的个人属性是可见的。。。接得好!我知道这肯定是件很愚蠢的事,比如忘了放一些盘子。难怪这些属性会以JS函数的形式出现。谢谢