Home > Software design >  Sort nested array of objects based on parent level and nested level
Sort nested array of objects based on parent level and nested level

Time:11-11

I want to sort an array of objects with parent category name and nested category name alphabetically.

[{"category_id":45,"category_name":"home ownership","subcategory":[{"category_id":364,"category_name":"test1","category_type":"EVENT"}]},{"category_id":22,"category_name":"Politics","subcategory":[{"category_id":69,"category_name":"Discussion","category_type":"EVENT"},{"category_id":70,"category_name":"Government Agencies","category_type":"EVENT"},{"category_id":71,"category_name":"Petitions","category_type":"EVENT"}]},{"category_id":174,"category_name":"german cultural events","subcategory":[{"category_id":285,"category_name":"Food1","category_type":"EVENT"},{"category_id":286,"category_name":"Food2","category_type":"EVENT"}]},{"category_id":18,"category_name":"Film & Media","subcategory":[{"category_id":47,"category_name":"Videography","category_type":"EVENT"},{"category_id":48,"category_name":"Producing","category_type":"EVENT"},{"category_id":50,"category_name":"Music","category_type":"EVENT"}]},{"category_id":146,"category_name":"Ask","subcategory":[{"category_id":275,"category_name":"Ask YourSelf","category_type":"EVENT"}]},{"category_id":136,"category_name":"Hello Testing","subcategory":[{"category_id":276,"category_name":"hi","category_type":"EVENT"}]},{"category_id":17,"category_name":"Fashion","subcategory":[{"category_id":41,"category_name":"Shopping","category_type":"EVENT"},{"category_id":42,"category_name":"Design","category_type":"EVENT"},{"category_id":45,"category_name":"Thrifty","category_type":"EVENT"},{"category_id":335,"category_name":"testManja","category_type":"EVENT"}]},{"category_id":234,"category_name":"Private","subcategory":[{"category_id":355,"category_name":"Private 1","category_type":"EVENT"}]},{"category_id":176,"category_name":"NewEvent","subcategory":[{"category_id":287,"category_name":"Event1","category_type":"EVENT"},{"category_id":288,"category_name":"Event2","category_type":"EVENT"}]},{"category_id":119,"category_name":"Art & Music","subcategory":[{"category_id":183,"category_name":"Arts & Culture","category_type":"EVENT"},{"category_id":187,"category_name":"testAAA","category_type":"EVENT"},{"category_id":316,"category_name":"Solo","category_type":"EVENT"},{"category_id":319,"category_name":"Art & Music","category_type":"EVENT"}]},{"category_id":21,"category_name":"Lifestyle","subcategory":[{"category_id":72,"category_name":"Recycling & Waste","category_type":"EVENT"},{"category_id":73,"category_name":"Tourism & Travel","category_type":"EVENT"}]},{"category_id":168,"category_name":"Event1","subcategory":[{"category_id":280,"category_name":"EventSub1","category_type":"EVENT"}]},{"category_id":25,"category_name":"Other","subcategory":[{"category_id":100,"category_name":"PC Building","category_type":"EVENT"}]},{"category_id":23,"category_name":"Spirituality","subcategory":[{"category_id":66,"category_name":"Religious Organizations","category_type":"EVENT"}]},{"category_id":15,"category_name":"Charity & Causes","subcategory":[{"category_id":33,"category_name":"Volunteer Work","category_type":"EVENT"},{"category_id":34,"category_name":"Social Causes","category_type":"EVENT"}]},{"category_id":170,"category_name":"Shahid","subcategory":[{"category_id":281,"category_name":"sub shahid1","category_type":"EVENT"},{"category_id":283,"category_name":"sub shahid2","category_type":"EVENT"}]},{"category_id":24,"category_name":"Sports & Rec","subcategory":[{"category_id":61,"category_name":"Sports Establishments","category_type":"EVENT"},{"category_id":64,"category_name":"Tournaments","category_type":"EVENT"},{"category_id":65,"category_name":"Fantasy","category_type":"EVENT"},{"category_id":317,"category_name":"Solo","category_type":"EVENT"},{"category_id":320,"category_name":"Art & Music","category_type":"EVENT"}]},{"category_id":137,"category_name":"Button Loading Testing","subcategory":[{"category_id":268,"category_name":"Heiya","category_type":"EVENT"},{"category_id":269,"category_name":"gaiya","category_type":"EVENT"}]},{"category_id":43,"category_name":"Boutique Events","subcategory":[{"category_id":133,"category_name":"clothes","category_type":"EVENT"}]},{"category_id":14,"category_name":"Beauty","subcategory":[{"category_id":29,"category_name":"Cosmetics Vj","category_type":"EVENT"},{"category_id":30,"category_name":"Makeup","category_type":"EVENT"},{"category_id":31,"category_name":"Hair","category_type":"EVENT"},{"category_id":32,"category_name":"Dermatology","category_type":"EVENT"},{"category_id":161,"category_name":"Beauty Products Demo","category_type":"EVENT"},{"category_id":162,"category_name":"Spa Events","category_type":"EVENT"},{"category_id":163,"category_name":"Hair and Skin Workshops","category_type":"EVENT"},{"category_id":164,"category_name":"Makeovers","category_type":"EVENT"},{"category_id":165,"category_name":"Beauty Products Sale","category_type":"EVENT"}]},{"category_id":16,"category_name":"Community","subcategory":[{"category_id":36,"category_name":"Economic Development","category_type":"EVENT"},{"category_id":37,"category_name":"Environmental","category_type":"EVENT"},{"category_id":38,"category_name":"Florists","category_type":"EVENT"}]},{"category_id":157,"category_name":"RehaEvent","subcategory":[{"category_id":279,"category_name":"reha 1","category_type":"EVENT"}]},{"category_id":166,"category_name":"CatReha","subcategory":[{"category_id":278,"category_name":"Reha1","category_type":"EVENT"},{"category_id":284,"category_name":"Reha2","category_type":"EVENT"}]},{"category_id":13,"category_name":"Business","subcategory":[{"category_id":12,"category_name":"Advertising","category_type":"EVENT"},{"category_id":13,"category_name":"Banks & Credit Unions","category_type":"EVENT"},{"category_id":14,"category_name":"Coworking","category_type":"EVENT"},{"category_id":15,"category_name":"Marketing","category_type":"EVENT"},{"category_id":16,"category_name":"Public Relations","category_type":"EVENT"},{"category_id":17,"category_name":"Financial Services","category_type":"EVENT"},{"category_id":18,"category_name":"Architects","category_type":"EVENT"},{"category_id":19,"category_name":"Designers","category_type":"EVENT"},{"category_id":20,"category_name":"Engineers","category_type":"EVENT"},{"category_id":21,"category_name":"Industrial","category_type":"EVENT"},{"category_id":22,"category_name":"Analysts","category_type":"EVENT"},{"category_id":23,"category_name":"Medical","category_type":"EVENT"},{"category_id":24,"category_name":"Management","category_type":"EVENT"},{"category_id":25,"category_name":"Insurance","category_type":"EVENT"},{"category_id":26,"category_name":"Law Firms & Legal","category_type":"EVENT"},{"category_id":27,"category_name":"Real Estate","category_type":"EVENT"},{"category_id":155,"category_name":"Education","category_type":"EVENT"},{"category_id":156,"category_name":"Job Fairs","category_type":"EVENT"},{"category_id":157,"category_name":"Finance","category_type":"EVENT"},{"category_id":158,"category_name":"Workshops","category_type":"EVENT"},{"category_id":159,"category_name":"Expos and Events","category_type":"EVENT"},{"category_id":160,"category_name":"Entrepreneurship","category_type":"EVENT"},{"category_id":274,"category_name":"Translation","category_type":"EVENT"}]},{"category_id":258,"category_name":"eVEnt Cat reha","subcategory":[]},{"category_id":266,"category_name":"TREKKING","subcategory":[{"category_id":371,"category_name":"SIMPLE TREKKING","category_type":"EVENT"},{"category_id":372,"category_name":"MODERATE TREKKING","category_type":"EVENT"}]}]

Output

[{"category_id":119,"category_name":"Art & Music","subcategory":[{"category_id":319,"category_name":"Art & Music","category_type":"EVENT"},{"category_id":183,"category_name":"Arts & Culture","category_type":"EVENT"},{"category_id":316,"category_name":"Solo","category_type":"EVENT"},{"category_id":187,"category_name":"testAAA","category_type":"EVENT"}]},{"category_id":146,"category_name":"Ask","subcategory":[{"category_id":275,"category_name":"Ask YourSelf","category_type":"EVENT"}]},{"category_id":14,"category_name":"Beauty","subcategory":[{"category_id":161,"category_name":"Beauty Products Demo","category_type":"EVENT"},{"category_id":165,"category_name":"Beauty Products Sale","category_type":"EVENT"},{"category_id":29,"category_name":"Cosmetics Vj","category_type":"EVENT"},{"category_id":32,"category_name":"Dermatology","category_type":"EVENT"},{"category_id":31,"category_name":"Hair","category_type":"EVENT"},{"category_id":163,"category_name":"Hair and Skin Workshops","category_type":"EVENT"},{"category_id":164,"category_name":"Makeovers","category_type":"EVENT"},{"category_id":30,"category_name":"Makeup","category_type":"EVENT"},{"category_id":162,"category_name":"Spa Events","category_type":"EVENT"}]},{"category_id":43,"category_name":"Boutique Events","subcategory":[{"category_id":133,"category_name":"clothes","category_type":"EVENT"}]},{"category_id":13,"category_name":"Business","subcategory":[{"category_id":12,"category_name":"Advertising","category_type":"EVENT"},{"category_id":22,"category_name":"Analysts","category_type":"EVENT"},{"category_id":18,"category_name":"Architects","category_type":"EVENT"},{"category_id":13,"category_name":"Banks & Credit Unions","category_type":"EVENT"},{"category_id":14,"category_name":"Coworking","category_type":"EVENT"},{"category_id":19,"category_name":"Designers","category_type":"EVENT"},{"category_id":155,"category_name":"Education","category_type":"EVENT"},{"category_id":20,"category_name":"Engineers","category_type":"EVENT"},{"category_id":160,"category_name":"Entrepreneurship","category_type":"EVENT"},{"category_id":159,"category_name":"Expos and Events","category_type":"EVENT"},{"category_id":157,"category_name":"Finance","category_type":"EVENT"},{"category_id":17,"category_name":"Financial Services","category_type":"EVENT"},{"category_id":21,"category_name":"Industrial","category_type":"EVENT"},{"category_id":25,"category_name":"Insurance","category_type":"EVENT"},{"category_id":156,"category_name":"Job Fairs","category_type":"EVENT"},{"category_id":26,"category_name":"Law Firms & Legal","category_type":"EVENT"},{"category_id":24,"category_name":"Management","category_type":"EVENT"},{"category_id":15,"category_name":"Marketing","category_type":"EVENT"},{"category_id":23,"category_name":"Medical","category_type":"EVENT"},{"category_id":16,"category_name":"Public Relations","category_type":"EVENT"},{"category_id":27,"category_name":"Real Estate","category_type":"EVENT"},{"category_id":274,"category_name":"Translation","category_type":"EVENT"},{"category_id":158,"category_name":"Workshops","category_type":"EVENT"}]},{"category_id":137,"category_name":"Button Loading Testing","subcategory":[{"category_id":268,"category_name":"Heiya","category_type":"EVENT"},{"category_id":269,"category_name":"gaiya","category_type":"EVENT"}]},{"category_id":166,"category_name":"CatReha","subcategory":[{"category_id":278,"category_name":"Reha1","category_type":"EVENT"},{"category_id":284,"category_name":"Reha2","category_type":"EVENT"}]},{"category_id":15,"category_name":"Charity & Causes","subcategory":[{"category_id":34,"category_name":"Social Causes","category_type":"EVENT"},{"category_id":33,"category_name":"Volunteer Work","category_type":"EVENT"}]},{"category_id":16,"category_name":"Community","subcategory":[{"category_id":36,"category_name":"Economic Development","category_type":"EVENT"},{"category_id":37,"category_name":"Environmental","category_type":"EVENT"},{"category_id":38,"category_name":"Florists","category_type":"EVENT"}]},{"category_id":258,"category_name":"eVEnt Cat reha","subcategory":[]},{"category_id":168,"category_name":"Event1","subcategory":[{"category_id":280,"category_name":"EventSub1","category_type":"EVENT"}]},{"category_id":17,"category_name":"Fashion","subcategory":[{"category_id":42,"category_name":"Design","category_type":"EVENT"},{"category_id":41,"category_name":"Shopping","category_type":"EVENT"},{"category_id":45,"category_name":"Thrifty","category_type":"EVENT"},{"category_id":335,"category_name":"testManja","category_type":"EVENT"}]},{"category_id":18,"category_name":"Film & Media","subcategory":[{"category_id":50,"category_name":"Music","category_type":"EVENT"},{"category_id":48,"category_name":"Producing","category_type":"EVENT"},{"category_id":47,"category_name":"Videography","category_type":"EVENT"}]},{"category_id":174,"category_name":"german cultural events","subcategory":[{"category_id":285,"category_name":"Food1","category_type":"EVENT"},{"category_id":286,"category_name":"Food2","category_type":"EVENT"}]},{"category_id":136,"category_name":"Hello Testing","subcategory":[{"category_id":276,"category_name":"hi","category_type":"EVENT"}]},{"category_id":45,"category_name":"home ownership","subcategory":[{"category_id":364,"category_name":"test1","category_type":"EVENT"}]},{"category_id":21,"category_name":"Lifestyle","subcategory":[{"category_id":72,"category_name":"Recycling & Waste","category_type":"EVENT"},{"category_id":73,"category_name":"Tourism & Travel","category_type":"EVENT"}]},{"category_id":176,"category_name":"NewEvent","subcategory":[{"category_id":287,"category_name":"Event1","category_type":"EVENT"},{"category_id":288,"category_name":"Event2","category_type":"EVENT"}]},{"category_id":25,"category_name":"Other","subcategory":[{"category_id":100,"category_name":"PC Building","category_type":"EVENT"}]},{"category_id":22,"category_name":"Politics","subcategory":[{"category_id":69,"category_name":"Discussion","category_type":"EVENT"},{"category_id":70,"category_name":"Government Agencies","category_type":"EVENT"},{"category_id":71,"category_name":"Petitions","category_type":"EVENT"}]},{"category_id":234,"category_name":"Private","subcategory":[{"category_id":355,"category_name":"Private 1","category_type":"EVENT"}]},{"category_id":157,"category_name":"RehaEvent","subcategory":[{"category_id":279,"category_name":"reha 1","category_type":"EVENT"}]},{"category_id":170,"category_name":"Shahid","subcategory":[{"category_id":281,"category_name":"sub shahid1","category_type":"EVENT"},{"category_id":283,"category_name":"sub shahid2","category_type":"EVENT"}]},{"category_id":23,"category_name":"Spirituality","subcategory":[{"category_id":66,"category_name":"Religious Organizations","category_type":"EVENT"}]},{"category_id":24,"category_name":"Sports & Rec","subcategory":[{"category_id":320,"category_name":"Art & Music","category_type":"EVENT"},{"category_id":65,"category_name":"Fantasy","category_type":"EVENT"},{"category_id":317,"category_name":"Solo","category_type":"EVENT"},{"category_id":61,"category_name":"Sports Establishments","category_type":"EVENT"},{"category_id":64,"category_name":"Tournaments","category_type":"EVENT"}]},{"category_id":266,"category_name":"TREKKING","subcategory":[{"category_id":372,"category_name":"MODERATE TREKKING","category_type":"EVENT"},{"category_id":371,"category_name":"SIMPLE TREKKING","category_type":"EVENT"}]}]

I have tries

formattedSubcategory.sort((a, b) => {
          if (a.category_name < b.category_name) {
            return -1
          }
          if (a.category_name > b.category_name) {
            return 1
          }
          return 0
          // a.category_name.localeCompare(b.category_name)
        })
return formatted.sort((a, b) =>
        a.category_name.localeCompare(b.category_name)
      )

but category named Button Loading Testing sub category did not sorted alphabetically, others are working fine, but this one don't

Sort should be

first category_name and second subcategories category_name

is there any good solutions please

CodePudding user response:

I'm assuming you want case insensitiveness. In that case:

arr.sort(byCategoryName).forEach(element => {
element.subcategory.sort(byCategoryName)
});

Full example (very long because of your array)

arr = [
  {
    category_id: 45,
    category_name: "home ownership",
    subcategory: [
      { category_id: 364, category_name: "test1", category_type: "EVENT" },
    ],
  },
  {
    category_id: 22,
    category_name: "Politics",
    subcategory: [
      { category_id: 69, category_name: "Discussion", category_type: "EVENT" },
      {
        category_id: 70,
        category_name: "Government Agencies",
        category_type: "EVENT",
      },
      { category_id: 71, category_name: "Petitions", category_type: "EVENT" },
    ],
  },
  {
    category_id: 174,
    category_name: "german cultural events",
    subcategory: [
      { category_id: 285, category_name: "Food1", category_type: "EVENT" },
      { category_id: 286, category_name: "Food2", category_type: "EVENT" },
    ],
  },
  {
    category_id: 18,
    category_name: "Film & Media",
    subcategory: [
      { category_id: 47, category_name: "Videography", category_type: "EVENT" },
      { category_id: 48, category_name: "Producing", category_type: "EVENT" },
      { category_id: 50, category_name: "Music", category_type: "EVENT" },
    ],
  },
  {
    category_id: 146,
    category_name: "Ask",
    subcategory: [
      {
        category_id: 275,
        category_name: "Ask YourSelf",
        category_type: "EVENT",
      },
    ],
  },
  {
    category_id: 136,
    category_name: "Hello Testing",
    subcategory: [
      { category_id: 276, category_name: "hi", category_type: "EVENT" },
    ],
  },
  {
    category_id: 17,
    category_name: "Fashion",
    subcategory: [
      { category_id: 41, category_name: "Shopping", category_type: "EVENT" },
      { category_id: 42, category_name: "Design", category_type: "EVENT" },
      { category_id: 45, category_name: "Thrifty", category_type: "EVENT" },
      { category_id: 335, category_name: "testManja", category_type: "EVENT" },
    ],
  },
  {
    category_id: 234,
    category_name: "Private",
    subcategory: [
      { category_id: 355, category_name: "Private 1", category_type: "EVENT" },
    ],
  },
  {
    category_id: 176,
    category_name: "NewEvent",
    subcategory: [
      { category_id: 287, category_name: "Event1", category_type: "EVENT" },
      { category_id: 288, category_name: "Event2", category_type: "EVENT" },
    ],
  },
  {
    category_id: 119,
    category_name: "Art & Music",
    subcategory: [
      {
        category_id: 183,
        category_name: "Arts & Culture",
        category_type: "EVENT",
      },
      { category_id: 187, category_name: "testAAA", category_type: "EVENT" },
      { category_id: 316, category_name: "Solo", category_type: "EVENT" },
      {
        category_id: 319,
        category_name: "Art & Music",
        category_type: "EVENT",
      },
    ],
  },
  {
    category_id: 21,
    category_name: "Lifestyle",
    subcategory: [
      {
        category_id: 72,
        category_name: "Recycling & Waste",
        category_type: "EVENT",
      },
      {
        category_id: 73,
        category_name: "Tourism & Travel",
        category_type: "EVENT",
      },
    ],
  },
  {
    category_id: 168,
    category_name: "Event1",
    subcategory: [
      { category_id: 280, category_name: "EventSub1", category_type: "EVENT" },
    ],
  },
  {
    category_id: 25,
    category_name: "Other",
    subcategory: [
      {
        category_id: 100,
        category_name: "PC Building",
        category_type: "EVENT",
      },
    ],
  },
  {
    category_id: 23,
    category_name: "Spirituality",
    subcategory: [
      {
        category_id: 66,
        category_name: "Religious Organizations",
        category_type: "EVENT",
      },
    ],
  },
  {
    category_id: 15,
    category_name: "Charity & Causes",
    subcategory: [
      {
        category_id: 33,
        category_name: "Volunteer Work",
        category_type: "EVENT",
      },
      {
        category_id: 34,
        category_name: "Social Causes",
        category_type: "EVENT",
      },
    ],
  },
  {
    category_id: 170,
    category_name: "Shahid",
    subcategory: [
      {
        category_id: 281,
        category_name: "sub shahid1",
        category_type: "EVENT",
      },
      {
        category_id: 283,
        category_name: "sub shahid2",
        category_type: "EVENT",
      },
    ],
  },
  {
    category_id: 24,
    category_name: "Sports & Rec",
    subcategory: [
      {
        category_id: 61,
        category_name: "Sports Establishments",
        category_type: "EVENT",
      },
      { category_id: 64, category_name: "Tournaments", category_type: "EVENT" },
      { category_id: 65, category_name: "Fantasy", category_type: "EVENT" },
      { category_id: 317, category_name: "Solo", category_type: "EVENT" },
      {
        category_id: 320,
        category_name: "Art & Music",
        category_type: "EVENT",
      },
    ],
  },
  {
    category_id: 137,
    category_name: "Button Loading Testing",
    subcategory: [
      { category_id: 268, category_name: "Heiya", category_type: "EVENT" },
      { category_id: 269, category_name: "gaiya", category_type: "EVENT" },
    ],
  },
  {
    category_id: 43,
    category_name: "Boutique Events",
    subcategory: [
      { category_id: 133, category_name: "clothes", category_type: "EVENT" },
    ],
  },
  {
    category_id: 14,
    category_name: "Beauty",
    subcategory: [
      {
        category_id: 29,
        category_name: "Cosmetics Vj",
        category_type: "EVENT",
      },
      { category_id: 30, category_name: "Makeup", category_type: "EVENT" },
      { category_id: 31, category_name: "Hair", category_type: "EVENT" },
      { category_id: 32, category_name: "Dermatology", category_type: "EVENT" },
      {
        category_id: 161,
        category_name: "Beauty Products Demo",
        category_type: "EVENT",
      },
      { category_id: 162, category_name: "Spa Events", category_type: "EVENT" },
      {
        category_id: 163,
        category_name: "Hair and Skin Workshops",
        category_type: "EVENT",
      },
      { category_id: 164, category_name: "Makeovers", category_type: "EVENT" },
      {
        category_id: 165,
        category_name: "Beauty Products Sale",
        category_type: "EVENT",
      },
    ],
  },
  {
    category_id: 16,
    category_name: "Community",
    subcategory: [
      {
        category_id: 36,
        category_name: "Economic Development",
        category_type: "EVENT",
      },
      {
        category_id: 37,
        category_name: "Environmental",
        category_type: "EVENT",
      },
      { category_id: 38, category_name: "Florists", category_type: "EVENT" },
    ],
  },
  {
    category_id: 157,
    category_name: "RehaEvent",
    subcategory: [
      { category_id: 279, category_name: "reha 1", category_type: "EVENT" },
    ],
  },
  {
    category_id: 166,
    category_name: "CatReha",
    subcategory: [
      { category_id: 278, category_name: "Reha1", category_type: "EVENT" },
      { category_id: 284, category_name: "Reha2", category_type: "EVENT" },
    ],
  },
  {
    category_id: 13,
    category_name: "Business",
    subcategory: [
      { category_id: 12, category_name: "Advertising", category_type: "EVENT" },
      {
        category_id: 13,
        category_name: "Banks & Credit Unions",
        category_type: "EVENT",
      },
      { category_id: 14, category_name: "Coworking", category_type: "EVENT" },
      { category_id: 15, category_name: "Marketing", category_type: "EVENT" },
      {
        category_id: 16,
        category_name: "Public Relations",
        category_type: "EVENT",
      },
      {
        category_id: 17,
        category_name: "Financial Services",
        category_type: "EVENT",
      },
      { category_id: 18, category_name: "Architects", category_type: "EVENT" },
      { category_id: 19, category_name: "Designers", category_type: "EVENT" },
      { category_id: 20, category_name: "Engineers", category_type: "EVENT" },
      { category_id: 21, category_name: "Industrial", category_type: "EVENT" },
      { category_id: 22, category_name: "Analysts", category_type: "EVENT" },
      { category_id: 23, category_name: "Medical", category_type: "EVENT" },
      { category_id: 24, category_name: "Management", category_type: "EVENT" },
      { category_id: 25, category_name: "Insurance", category_type: "EVENT" },
      {
        category_id: 26,
        category_name: "Law Firms & Legal",
        category_type: "EVENT",
      },
      { category_id: 27, category_name: "Real Estate", category_type: "EVENT" },
      { category_id: 155, category_name: "Education", category_type: "EVENT" },
      { category_id: 156, category_name: "Job Fairs", category_type: "EVENT" },
      { category_id: 157, category_name: "Finance", category_type: "EVENT" },
      { category_id: 158, category_name: "Workshops", category_type: "EVENT" },
      {
        category_id: 159,
        category_name: "Expos and Events",
        category_type: "EVENT",
      },
      {
        category_id: 160,
        category_name: "Entrepreneurship",
        category_type: "EVENT",
      },
      {
        category_id: 274,
        category_name: "Translation",
        category_type: "EVENT",
      },
    ],
  },
  { category_id: 258, category_name: "eVEnt Cat reha", subcategory: [] },
  {
    category_id: 266,
    category_name: "TREKKING",
    subcategory: [
      {
        category_id: 371,
        category_name: "SIMPLE TREKKING",
        category_type: "EVENT",
      },
      {
        category_id: 372,
        category_name: "MODERATE TREKKING",
        category_type: "EVENT",
      },
    ],
  },
]


const byCategoryName = (a,b) => a.category_name.localeCompare(b.category_name);

arr.sort(byCategoryName).forEach(element => {
    element.subcategory.sort(byCategoryName)
});

console.log(arr)
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related