3

I have JSON like this inside the main Array Object

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

I want to sort the above object like

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

what I have tried so far

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

How to track title on my main Array

alert(thisObj.scheduleSubject[0].sectionRange[0].items[0].title);

I want to sort all the items on the base of its title digits before - character example 1- , 2- ,3- ,4-, 5-, 6-, 21-,56- and so on.

Main Array Structure

[
  {
    "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 Answers 3

2

You can split on that char and sort via that (assuming your main data structure is named 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];
        });
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your answer I will check and let you know
1

Just extract those number values and let orderBy filter of angularjs do the job for you

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 takes a second parameter (true / false) for asc / desc ordering

Demo

6 Comments

for simplicity purposes I only included items part of your object in demo
the above answered solved my problem but I also tried your method and I am getting an error. Token 'orderBy' is an unexpected token at column 17 of the expression [container.items orderBy:getNumber(item):true] starting at [orderBy:getNumber(item):true].
most probably you copied something wrong, please compare your code with the example provided in plnkr demo. Also if you can create a plunker I can help you with debugging.
yes, I just saw the example and now it is working too.
glad i was able to help.
|
1

You can use a Array.prototype.sort with a simple sort function in which you parse the title value of each object in the array as an int and compare. Something like this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.