Ich habe JSON wie dieses im Haupt-Array-Objekt

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

Ich möchte das obige Objekt wie sortieren

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

Was ich bisher versucht habe

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

So verfolgen Sie den Titel auf meinem Hauptarray

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

Ich möchte alle Elemente auf der Basis ihrer Titelziffern vor sortieren - Zeichenbeispiel 1-, 2-, 3-, 4-, 5-, 6-, 21-, 56- und so weiter.

Hauptarray-Struktur

[
  {
    "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
Sayed Mohd Ali 18 Jän. 2019 im 17:26

3 Antworten

Beste Antwort

Sie können split auf diesem Zeichen {sortieren und danach sortieren (vorausgesetzt, Ihre Hauptdatenstruktur heißt 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];
        });
    });
});
2
tymeJV 18 Jän. 2019 im 14:33