@@ -260,29 +260,40 @@ export class SortService {
260
260
261
261
sortComparer ( sortColumns : ColumnSort [ ] , dataRow1 : any , dataRow2 : any ) {
262
262
if ( Array . isArray ( sortColumns ) ) {
263
- for ( let i = 0 , l = sortColumns . length ; i < l ; i ++ ) {
264
- const columnSortObj = sortColumns [ i ] ;
265
- if ( columnSortObj && columnSortObj . sortCol ) {
266
- const sortDirection = columnSortObj . sortAsc ? SortDirectionNumber . asc : SortDirectionNumber . desc ;
267
- const sortField = columnSortObj . sortCol . queryFieldSorter || columnSortObj . sortCol . queryField || columnSortObj . sortCol . field ;
268
- const fieldType = columnSortObj . sortCol . type || FieldType . string ;
269
- let value1 = dataRow1 [ sortField ] ;
270
- let value2 = dataRow2 [ sortField ] ;
263
+ for ( const sortColumn of sortColumns ) {
264
+ if ( sortColumn && sortColumn . sortCol ) {
265
+ const columnDef = sortColumn . sortCol ;
266
+ const querySortField = sortColumn . sortCol ;
267
+ const sortDirection = sortColumn . sortAsc ? SortDirectionNumber . asc : SortDirectionNumber . desc ;
268
+ let queryFieldName1 = querySortField . queryFieldSorter || querySortField . queryField || querySortField . field ;
269
+ let queryFieldName2 = queryFieldName1 ;
270
+ const fieldType = querySortField . type || FieldType . string ;
271
+
272
+ // if user provided a query field name getter callback, we need to get the name on each item independently
273
+ if ( typeof columnDef . queryFieldNameGetterFn === 'function' ) {
274
+ queryFieldName1 = columnDef . queryFieldNameGetterFn ( dataRow1 ) ;
275
+ queryFieldName2 = columnDef . queryFieldNameGetterFn ( dataRow2 ) ;
276
+ }
277
+
278
+ let value1 = dataRow1 [ queryFieldName1 ] ;
279
+ let value2 = dataRow2 [ queryFieldName2 ] ;
271
280
272
281
// when item is a complex object (dot "." notation), we need to filter the value contained in the object tree
273
- if ( sortField && sortField . indexOf ( '.' ) >= 0 ) {
274
- value1 = getDescendantProperty ( dataRow1 , sortField ) ;
275
- value2 = getDescendantProperty ( dataRow2 , sortField ) ;
282
+ if ( queryFieldName1 && queryFieldName1 . indexOf ( '.' ) >= 0 ) {
283
+ value1 = getDescendantProperty ( dataRow1 , queryFieldName1 ) ;
284
+ }
285
+ if ( queryFieldName2 && queryFieldName2 . indexOf ( '.' ) >= 0 ) {
286
+ value2 = getDescendantProperty ( dataRow2 , queryFieldName2 ) ;
276
287
}
277
288
278
289
// user could provide his own custom Sorter
279
- if ( columnSortObj . sortCol && columnSortObj . sortCol . sorter ) {
280
- const customSortResult = columnSortObj . sortCol . sorter ( value1 , value2 , sortDirection , columnSortObj . sortCol ) ;
290
+ if ( querySortField && querySortField . sorter ) {
291
+ const customSortResult = querySortField . sorter ( value1 , value2 , sortDirection , querySortField ) ;
281
292
if ( customSortResult !== SortDirectionNumber . neutral ) {
282
293
return customSortResult ;
283
294
}
284
295
} else {
285
- const sortResult = sortByFieldType ( fieldType , value1 , value2 , sortDirection , columnSortObj . sortCol ) ;
296
+ const sortResult = sortByFieldType ( fieldType , value1 , value2 , sortDirection , querySortField ) ;
286
297
if ( sortResult !== SortDirectionNumber . neutral ) {
287
298
return sortResult ;
288
299
}
0 commit comments