I wrote generic function for sorting array in typescript
export function sortArray(incomingArray: any[], sortField: string = '', sortOrder: string = 'asc'): any[] {
if (!sortField) {
return incomingArray;
}
let result: any[] = [];
result = incomingArray.sort((a, b) => {
let testData = a.ohlc['priceInUsd'];
if (a[sortField] < b[sortField]) {
return sortOrder === 'asc' ? -1 : 1;
}
if (a[sortField] > b[sortField]) {
return sortOrder === 'asc' ? 1 : -1;
}
return 0;
});
return result;
}
Wokring perfectly fine till structure is inside one array. Problem is if I have more complex data like in photo and then "a[sortField]
" doesn't wok well any more
How to chnage "a[sortField]
" that will be more generic if I have more complex data and not need to write "a.x[sortField]
"