AngularJs对象排序

3

我有一个主数组对象,里面包含如下JSON:

obj = [{{"title":"1-Introduction"},
       {"title":"5-Introduction"},
       {"title":"20-Introduction"},
       {"title":"4-Introduction"} }]

我希望对上述对象进行排序,例如:
obj = [{{"title":"1-Introduction"},
       {"title":"4-Introduction"},
       {"title":"5-Introduction"},
       {"title":"20-Introduction"} }]

我目前尝试过的方法

$scope.checkWeightage=function(){
            thisObj.scheduleSubject.map(itm=>itm.sectionRange.map(subItm=>{
                    var min = subItm.chapterActualWeightage-(subItm.chapterActualWeightage/10);
                    var max = subItm.chapterActualWeightage+(subItm.chapterActualWeightage/10);
                    var sum = subItm.items.reduce((total,it)=>{
                       return total+it.actualWeightage;
                    },0);
                    subItm['weightageError'] = (sum>max || sum<min)?true:false;
                    subItm['ChapterActualWeightageCurrently'] = parseFloat(Math.round(sum*100)/100);
                    subItm.items.sort((a,b)=>a.title.split(/_(.+)/)[0]>b.title.split(/_(.+)/)[0]);
                })
            ); console.log(thisObj.scheduleSubject[0].chapterData); //.1[0].title);
            //console.log("CHECK weightage",thisObj.scheduleSubject);
        }

如何跟踪我的主数组标题
alert(thisObj.scheduleSubject[0].sectionRange[0].items[0].title);

我希望能按照标题数字(在“-”字符前)对所有项进行排序,例如1-、2-、3-、4-、5-、6-、21-、56-等等。
主数组结构。
[
  {
    "id": "25",
    "section": "1",
    "sectionRange": [
      {
        "sectionNumber": 1,
        "effectAllowed": "all",
        "Date": "",
        "items": [
          {
            "subjectId": 25,
            "section": 1,
            "chapterId": 283,
            "actualWeightage": 3.42,
            "totalPaperMarks": 132,
            "title": "10-Creation & Registration of Charges",
            "$$hashKey": "object:146"
          },
          {
            "subjectId": 25,
            "section": 1,
            "chapterId": 284,
            "actualWeightage": 2.23,
            "totalPaperMarks": 132,
            "title": "11-Allotment of Securities & Issue of Certificates",
            "$$hashKey": "object:147"
          },
          {
            "subjectId": 25,
            "section": 1,
            "chapterId": 285,
            "actualWeightage": 1.37,
            "totalPaperMarks": 132,
            "title": "12-Membership in a Company",
            "$$hashKey": "object:148"
          },
          {
            "subjectId": 25,
            "section": 1,
            "chapterId": 286,
            "actualWeightage": 3.42,
            "totalPaperMarks": 132,
            "title": "13-Transfer & Transmission of Securities",
            "$$hashKey": "object:149"
          },
          {
            "subjectId": 25,
            "section": 1,
            "chapterId": 287,
            "actualWeightage": 7.53,
            "totalPaperMarks": 132,
            "title": "14-Institution of Directors",
            "$$hashKey": "object:150"
          },
          {
            "subjectId": 25,
            "section": 1,
            "chapterId": 288,
            "actualWeightage": 1.37,
            "totalPaperMarks": 132,
            "title": "15-Independent Directors",
            "$$hashKey": "object:151"
          },
          {
            "subjectId": 25,
            "section": 1,
            "chapterId": 289,
            "actualWeightage": 13.35,
            "totalPaperMarks": 132,
            "title": "16-Board & its Powers",
            "$$hashKey": "object:152"
          },
          {
            "subjectId": 25,
            "section": 1,
            "chapterId": 290,
            "actualWeightage": 8.22,
            "totalPaperMarks": 132,
            "title": "17-Appointment & Remuneration of Key Managerial Personnel",
            "$$hashKey": "object:153"
          },
          {
            "subjectId": 25,
            "section": 1,
            "chapterId": 291,
            "actualWeightage": 6.68,
            "totalPaperMarks": 132,
            "title": "18-General Meetings",
            "$$hashKey": "object:154"
          },
          {
            "subjectId": 25,
            "section": 1,
            "chapterId": 292,
            "actualWeightage": 1.37,
            "totalPaperMarks": 132,
            "title": "19-Loans & Investments by Companies",
            "$$hashKey": "object:155"
          }
3个回答

2
你可以在该字符上进行split操作,并通过该字符进行排序(假设你的主要数据结构命名为structure):
structure.forEach(s => {
    s.sectionRange.forEach(sr => {
        sr.items.sort(function(a, b) {
            let aParts = a.split("-"),
                bParts = b.split("-");

            return +aParts[0] - +bParts[0];
        });
    });
});

谢谢您的回答,我会检查并告诉您。 - Sayed Mohd Ali

1
你可以使用一个简单的sort函数,结合Array.prototype.sort方法,将数组中每个对象的title值解析为整数并进行比较。示例代码如下:

var arr = [{
    "title": "1-Introduction"
  },
  {
    "title": "5-Introduction"
  },
  {
    "title": "20-Introduction"
  },
  {
    "title": "4-Introduction"
  }
];


arr.sort(function(a, b) {
  const aVal = parseInt(a.title.split("-")[0]);
  const bVal = parseInt(b.title.split("-")[0]);
  return aVal - bVal;
});

console.log(arr);


1
只需提取这些数字值,让angularjs的orderBy过滤器为您完成工作。
JS
  $scope.getNumber = function(row){
    var value = row.title.split("-")[0];
    return parseInt(value);
  };

html

<div ng-repeat="item in data | orderBy:getNumber:false">{{item.title}}</div>

also orderBy接受第二个参数(true / false)进行升序/降序排序。

演示


为简单起见,我仅包括演示中对象的部分。 - NTP
上面的答案解决了我的问题,但我也尝试了你的方法,结果出现了错误。在表达式[container.items orderBy:getNumber(item):true]中,标记“orderBy”是一个意外的标记,位于第17列,起始位置为[orderBy:getNumber(item):true]。 - Sayed Mohd Ali
很可能你复制错了,请将你的代码与plnkr演示中提供的示例进行比较。如果你能创建一个plunker,我可以帮你调试。 - NTP
是的,我刚刚看了这个例子,现在它也可以工作了。 - Sayed Mohd Ali
很高兴我能帮到你。 - NTP
我认为与上述方法相比,这种排序方法会更慢。我对吗? - Sayed Mohd Ali

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接