@@ -39,8 +39,8 @@ import {StyleUtils} from '../../utils/styling/style-utils';
3939 [fxLayoutGap.gt-xs], [fxLayoutGap.gt-sm], [fxLayoutGap.gt-md], [fxLayoutGap.gt-lg]
4040`
4141} )
42- export class LayoutGapDirective extends BaseFxDirective implements AfterContentInit , OnChanges ,
43- OnDestroy {
42+ export class LayoutGapDirective extends BaseFxDirective
43+ implements AfterContentInit , OnChanges , OnDestroy {
4444 protected _layout = 'row' ; // default flex-direction
4545 protected _layoutWatcher : Subscription ;
4646 protected _observer : MutationObserver ;
@@ -128,7 +128,7 @@ export class LayoutGapDirective extends BaseFxDirective implements AfterContentI
128128
129129 if ( typeof MutationObserver !== 'undefined' ) {
130130 this . _observer = new MutationObserver ( ( mutations : MutationRecord [ ] ) => {
131- let validatedChanges = ( it : MutationRecord ) : boolean => {
131+ const validatedChanges = ( it : MutationRecord ) : boolean => {
132132 return ( it . addedNodes && it . addedNodes . length > 0 ) ||
133133 ( it . removedNodes && it . removedNodes . length > 0 ) ;
134134 } ;
@@ -164,16 +164,23 @@ export class LayoutGapDirective extends BaseFxDirective implements AfterContentI
164164 }
165165
166166 // Gather all non-hidden Element nodes
167- let items = this . childrenNodes
168- . filter ( el => el . nodeType === 1 && this . _getDisplayStyle ( el ) != 'none' ) ;
169- let numItems = items . length ;
170-
171- if ( numItems > 0 ) {
172- let lastItem = items [ numItems - 1 ] ;
167+ const items = this . childrenNodes
168+ . filter ( el => el . nodeType === 1 && this . _getDisplayStyle ( el ) != 'none' )
169+ . sort ( ( a , b ) => {
170+ const orderA = + this . _styler . lookupStyle ( a , 'order' ) ;
171+ const orderB = + this . _styler . lookupStyle ( b , 'order' ) ;
172+ if ( isNaN ( orderA ) || isNaN ( orderB ) || orderA === orderB ) {
173+ return 0 ;
174+ } else {
175+ return orderA > orderB ? 1 : - 1 ;
176+ }
177+ } ) ;
178+
179+ if ( items . length > 0 ) {
180+ const lastItem = items . pop ( ) ;
173181
174182 // For each `element` children EXCEPT the last,
175183 // set the margin right/bottom styles...
176- items = items . filter ( ( _ , j ) => j < numItems - 1 ) ;
177184 this . _applyStyleToElements ( this . _buildCSS ( value ) , items ) ;
178185
179186 // Clear all gaps for all visible elements
0 commit comments