1- import { cloneArray , resolveNestedPath } from '../core/utils' ;
1+ import { cloneArray , resolveNestedPath , parseDate } from '../core/utils' ;
22import { IGroupByRecord } from './groupby-record.interface' ;
33import { ISortingExpression , SortingDirection } from './sorting-expression.interface' ;
44import { IGroupingExpression } from './grouping-expression.interface' ;
55import { IGroupingState } from './groupby-state.interface' ;
66import { IGroupByExpandState } from './groupby-expand-state.interface' ;
77import { IGroupByResult } from './grouping-result.interface' ;
88import { getHierarchy , isHierarchyMatch } from './operations' ;
9+ import { GridType } from '../grids/common/grid.interface' ;
10+
11+ const DATE_TYPE = 'date' ;
912
1013export interface ISortingStrategy {
1114 sort : ( data : any [ ] ,
1215 fieldName : string ,
1316 dir : SortingDirection ,
1417 ignoreCase : boolean ,
15- valueResolver : ( obj : any , key : string ) => any ) => any [ ] ;
18+ valueResolver : ( obj : any , key : string , isDate ?: boolean ) => any ,
19+ isDate ?: boolean ) => any [ ] ;
1620}
1721
1822export class DefaultSortingStrategy implements ISortingStrategy {
@@ -28,11 +32,12 @@ export class DefaultSortingStrategy implements ISortingStrategy {
2832 fieldName : string ,
2933 dir : SortingDirection ,
3034 ignoreCase : boolean ,
31- valueResolver : ( obj : any , key : string ) => any ) {
35+ valueResolver : ( obj : any , key : string , isDate ?: boolean ) => any ,
36+ isDate ?: boolean ) {
3237 const key = fieldName ;
3338 const reverse = ( dir === SortingDirection . Desc ? - 1 : 1 ) ;
3439 const cmpFunc = ( obj1 , obj2 ) => {
35- return this . compareObjects ( obj1 , obj2 , key , reverse , ignoreCase , valueResolver ) ;
40+ return this . compareObjects ( obj1 , obj2 , key , reverse , ignoreCase , valueResolver , isDate ) ;
3641 } ;
3742 return this . arraySort ( data , cmpFunc ) ;
3843 }
@@ -56,9 +61,10 @@ export class DefaultSortingStrategy implements ISortingStrategy {
5661 key : string ,
5762 reverse : number ,
5863 ignoreCase : boolean ,
59- valueResolver : ( obj : any , key : string ) => any ) {
60- let a = valueResolver ( obj1 , key ) ;
61- let b = valueResolver ( obj2 , key ) ;
64+ valueResolver : ( obj : any , key : string , isDate ?: boolean ) => any ,
65+ isDate : boolean = false ) {
66+ let a = valueResolver ( obj1 , key , isDate ) ;
67+ let b = valueResolver ( obj2 , key , isDate ) ;
6268 if ( ignoreCase ) {
6369 a = a && a . toLowerCase ? a . toLowerCase ( ) : a ;
6470 b = b && b . toLowerCase ? b . toLowerCase ( ) : b ;
@@ -72,7 +78,7 @@ export class DefaultSortingStrategy implements ISortingStrategy {
7278}
7379
7480export interface IGridSortingStrategy {
75- sort ( data : any [ ] , expressions : ISortingExpression [ ] ) : any [ ] ;
81+ sort ( data : any [ ] , expressions : ISortingExpression [ ] , grid ?: GridType ) : any [ ] ;
7682}
7783
7884export class NoopSortingStrategy implements IGridSortingStrategy {
@@ -90,24 +96,25 @@ export class NoopSortingStrategy implements IGridSortingStrategy {
9096}
9197
9298export class IgxSorting implements IGridSortingStrategy {
93- public sort ( data : any [ ] , expressions : ISortingExpression [ ] ) : any [ ] {
94- return this . sortDataRecursive ( data , expressions ) ;
99+ public sort ( data : any [ ] , expressions : ISortingExpression [ ] , grid ?: GridType ) : any [ ] {
100+ return this . sortDataRecursive ( data , expressions , 0 , grid ) ;
95101 }
96102
97103 private groupedRecordsByExpression ( data : any [ ] ,
98104 index : number ,
99- expression : IGroupingExpression ) : any [ ] {
105+ expression : IGroupingExpression ,
106+ isDate : boolean = false ) : any [ ] {
100107 let i ;
101108 let groupval ;
102109 const res = [ ] ;
103110 const key = expression . fieldName ;
104111 const len = data . length ;
105112 res . push ( data [ index ] ) ;
106- groupval = this . getFieldValue ( data [ index ] , key ) ;
113+ groupval = this . getFieldValue ( data [ index ] , key , isDate ) ;
107114 index ++ ;
108115 const comparer = expression . groupingComparer || DefaultSortingStrategy . instance ( ) . compareValues ;
109116 for ( i = index ; i < len ; i ++ ) {
110- if ( comparer ( this . getFieldValue ( data [ i ] , key ) , groupval ) === 0 ) {
117+ if ( comparer ( this . getFieldValue ( data [ i ] , key , isDate ) , groupval ) === 0 ) {
111118 res . push ( data [ i ] ) ;
112119 } else {
113120 break ;
@@ -117,7 +124,8 @@ export class IgxSorting implements IGridSortingStrategy {
117124 }
118125 private sortDataRecursive < T > ( data : T [ ] ,
119126 expressions : ISortingExpression [ ] ,
120- expressionIndex : number = 0 ) : T [ ] {
127+ expressionIndex : number = 0 ,
128+ grid : GridType ) : T [ ] {
121129 let i ;
122130 let j ;
123131 let expr : ISortingExpression ;
@@ -133,16 +141,18 @@ export class IgxSorting implements IGridSortingStrategy {
133141 if ( ! expr . strategy ) {
134142 expr . strategy = DefaultSortingStrategy . instance ( ) ;
135143 }
136- data = expr . strategy . sort ( data , expr . fieldName , expr . dir , expr . ignoreCase , this . getFieldValue ) ;
144+ const isDate = grid && grid . getColumnByName ( expr . fieldName ) ?
145+ grid . getColumnByName ( expr . fieldName ) . dataType === DATE_TYPE : false ;
146+ data = expr . strategy . sort ( data , expr . fieldName , expr . dir , expr . ignoreCase , this . getFieldValue , isDate ) ;
137147 if ( expressionIndex === exprsLen - 1 ) {
138148 return data ;
139149 }
140150 // in case of multiple sorting
141151 for ( i = 0 ; i < dataLen ; i ++ ) {
142- gbData = this . groupedRecordsByExpression ( data , i , expr ) ;
152+ gbData = this . groupedRecordsByExpression ( data , i , expr , isDate ) ;
143153 gbDataLen = gbData . length ;
144154 if ( gbDataLen > 1 ) {
145- gbData = this . sortDataRecursive ( gbData , expressions , expressionIndex + 1 ) ;
155+ gbData = this . sortDataRecursive ( gbData , expressions , expressionIndex + 1 , grid ) ;
146156 }
147157 for ( j = 0 ; j < gbDataLen ; j ++ ) {
148158 data [ i + j ] = gbData [ j ] ;
@@ -152,20 +162,21 @@ export class IgxSorting implements IGridSortingStrategy {
152162 return data ;
153163 }
154164 protected groupDataRecursive < T > ( data : T [ ] , state : IGroupingState , level : number ,
155- parent : IGroupByRecord , metadata : IGroupByRecord [ ] , grid : any = null ,
165+ parent : IGroupByRecord , metadata : IGroupByRecord [ ] , grid : GridType = null ,
156166 groupsRecords : any [ ] = [ ] , fullResult : IGroupByResult = { data : [ ] , metadata : [ ] } ) : T [ ] {
157167 const expressions = state . expressions ;
158168 const expansion = state . expansion ;
159169 let i = 0 ;
160170 let result = [ ] ;
161171 while ( i < data . length ) {
162- const group = this . groupedRecordsByExpression ( data , i , expressions [ level ] ) ;
163172 const column = grid ? grid . getColumnByName ( expressions [ level ] . fieldName ) : null ;
173+ const isDate = column ?. dataType === DATE_TYPE ;
174+ const group = this . groupedRecordsByExpression ( data , i , expressions [ level ] , isDate ) ;
164175 const groupRow : IGroupByRecord = {
165176 expression : expressions [ level ] ,
166177 level,
167178 records : cloneArray ( group ) ,
168- value : this . getFieldValue ( group [ 0 ] , expressions [ level ] . fieldName ) ,
179+ value : this . getFieldValue ( group [ 0 ] , expressions [ level ] . fieldName , isDate ) ,
169180 groupParent : parent ,
170181 groups : [ ] ,
171182 height : grid ? grid . renderedRowHeight : null ,
@@ -205,14 +216,14 @@ export class IgxSorting implements IGridSortingStrategy {
205216 }
206217 return result ;
207218 }
208- protected getFieldValue ( obj : any , key : string ) : any {
209- return resolveNestedPath ( obj , key ) ;
219+ protected getFieldValue ( obj : any , key : string , isDate : boolean = false ) : any {
220+ return isDate ? parseDate ( resolveNestedPath ( obj , key ) ) : resolveNestedPath ( obj , key ) ;
210221 }
211222}
212223
213224export class IgxDataRecordSorting extends IgxSorting {
214225
215- protected getFieldValue ( obj : any , key : string ) : any {
216- return resolveNestedPath ( obj . data , key ) ;
226+ protected getFieldValue ( obj : any , key : string , isDate : boolean = false ) : any {
227+ return isDate ? parseDate ( resolveNestedPath ( obj . data , key ) ) : resolveNestedPath ( obj . data , key ) ;
217228 }
218229}
0 commit comments