@@ -3850,6 +3850,29 @@ describe('MatSelect', () => {
38503850 expect ( fixture . componentInstance . control . value ) . toEqual ( [ 'steak-0' , 'pizza-1' , 'tacos-2' ] ) ;
38513851 } ) ) ;
38523852
3853+ it ( 'should be able to customize the value sorting logic' , fakeAsync ( ( ) => {
3854+ fixture . componentInstance . sortComparator = ( a , b , optionsArray ) => {
3855+ return optionsArray . indexOf ( b ) - optionsArray . indexOf ( a ) ;
3856+ } ;
3857+ fixture . detectChanges ( ) ;
3858+
3859+ trigger . click ( ) ;
3860+ fixture . detectChanges ( ) ;
3861+ flush ( ) ;
3862+
3863+ const options = overlayContainerElement . querySelectorAll ( 'mat-option' ) as
3864+ NodeListOf < HTMLElement > ;
3865+
3866+ for ( let i = 0 ; i < 3 ; i ++ ) {
3867+ options [ i ] . click ( ) ;
3868+ }
3869+ fixture . detectChanges ( ) ;
3870+
3871+ // Expect the items to be in reverse order.
3872+ expect ( trigger . textContent ) . toContain ( 'Tacos, Pizza, Steak' ) ;
3873+ expect ( fixture . componentInstance . control . value ) . toEqual ( [ 'tacos-2' , 'pizza-1' , 'steak-0' ] ) ;
3874+ } ) ) ;
3875+
38533876 it ( 'should sort the values that get set via the model based on the panel order' ,
38543877 fakeAsync ( ( ) => {
38553878 trigger . click ( ) ;
@@ -4320,7 +4343,8 @@ class FloatLabelSelect {
43204343 selector : 'multi-select' ,
43214344 template : `
43224345 <mat-form-field>
4323- <mat-select multiple placeholder="Food" [formControl]="control">
4346+ <mat-select multiple placeholder="Food" [formControl]="control"
4347+ [sortComparator]="sortComparator">
43244348 <mat-option *ngFor="let food of foods"
43254349 [value]="food.value">{{ food.viewValue }}
43264350 </mat-option>
@@ -4343,6 +4367,7 @@ class MultiSelect {
43434367
43444368 @ViewChild ( MatSelect ) select : MatSelect ;
43454369 @ViewChildren ( MatOption ) options : QueryList < MatOption > ;
4370+ sortComparator : ( a : MatOption , b : MatOption , options : MatOption [ ] ) => number ;
43464371}
43474372
43484373@Component ( {
0 commit comments