Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

392
Views
sort the menu alphabetically in angular

am trying to figure out if there's a to sort the items(buttons) inside the menu in alphabetical order by means of a function in the typescript file, please the following code which uses angular material.

<mat-menu #menu3="matMenu" [overlapTrigger]="false">
   <button mat-menu-item [routerLink]="['A']">A</button>
   <button mat-menu-item [routerLink]="['C']">C</button>
   <button mat-menu-item [routerLink]="['D']">D</button>
   <button mat-menu-item [routerLink]="['B']">B</button>  
</mat-menu>
about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

You can add the menu items in an array, sort the array, then render it using *ngFor , smth similar to:

public menuItems: string[] = ["C", "B", "A"];
// use sort() to sort the array in ngOnInit() or wherever convenient
ngOnInit() {
   this.menuItems = this.menuItems.sort();
}

then in the template bind the soreted array to *ngFor

<mat-menu #menu3="matMenu" [overlapTrigger]="false">
   <button mat-menu-item
           *ngFor="let item of menuItems"
           [routerLink]="[item]">{{ item }} 
    </button> 
</mat-menu>

Check MDN for sort() docs Here for custom sorting, etc ...

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!