import stringify from "json-stable-stringify" ; export type ArrayKeys < T > = { [ K in keyof T ] : Required < T > [ K ] extends any [ ] ? K : never ; } [ keyof T ] ; param obj param keys export function sortArraysInObject < T extends object > ( obj , keysIn ? , ignoreNonArrays = false ) { let keys ; keys = keysIn ? keysIn . map ( ( v ) => v . toString ( ) ) : Object . keys ( obj ) ; if ( ! keysIn ) ignoreNonArrays = true ; for ( const k of keys ) { const arr = ( obj as any ) [ k ] ; if ( arr === undefined ) continue ; if ( ! Array . isArray ( arr ) ) { if ( ignoreNonArrays ) { continue ; } else { throw new Error ( `Unable to sort non-array (key=${ k })` ) ; } } if ( arr . length === 0 ) continue ; sortArray ( arr ) ; } } export function sortArray < T > ( arr ) { if ( typeof arr [ 0 ] === "string" ) arr . sort ( ) ; else { arr . sort ( ( a , b ) => { const aS = stringify ( a ) ; const bS = stringify ( b ) ; return a === b ? 0 : aS < bS ? - 1 : 1 ; } ) ; } return arr ; }