对复杂的对象和对象数组进行排序再次包含一个数组

时间:2018-02-26 11:31:40

标签: javascript

我知道排序问题很多时候被问到,但是我遇到了一个不同的问题,我已经搜索并尝试了不同的东西,但是没有成功排序我的数组。有人可以帮助我,你的帮助将是非常可观的。我是编程新手。

这是我的数组: -

var People = [
        {Name: [ {name: "Name", surname: "100-150"},
                 {name: "AAA", surname: "1-10"},
                 {name: "Name", surname: "251-300"},
                {name: "Name", surname: "10-99"},
                {name: "Name", surname: "201-250"},
                {name: "Name", surname: "152-200"}], Middlename: "JJ"},

          {Name: [ {name: "Name", surname: "100-150"},
                 {name: "AAA", surname: "1-10"},
                 {name: "Name", surname: "251-300"},
                {name: "Name", surname: "10-99"},
                {name: "Name", surname: "201-250"},
                {name: "Name", surname: "152-200"}], Middlename:"Abrams"},
          {Name: [ {name: "Name", surname: "100-150"},
                 {name: "AAA", surname: "1-10"},
                 {name: "Name", surname: "251-300"},
                {name: "Name", surname: "10-99"},
                {name: "Name", surname: "201-250"},
                {name: "Name", surname: "152-200"}], Middlename: "Wars"},
         {Name: [ {name: "Name", surname: "100-150"},
                 {name: "AAA", surname: "1-10"},
                 {name: "Name", surname: "251-300"},
                {name: "Name", surname: "10-99"},
                {name: "Name", surname: "201-250"},
                {name: "Name", surname: "152-200"}], Middlename: "Wars"}

];

这里我尝试过: -

doSorting: function(People) {

        for(var x = 0; x < People.length; x++){
          for(var y=0;y<People.Name.length; y ++){
            People.Name[y] = People.Name[y].sort(function(obj1,obj2){
              return obj1.surname.split("-")[0] - obj2.surname.split("-")[0];
            });
          }

        }

        return People;
    }
    console.log(People);

0 个答案:

没有答案