@keplr-ewallet/ewallet-sdk-cosmos
Version:
23 lines (17 loc) • 524 B
text/typescript
export function sortObjectByKey(obj: Record<string, any>): any {
if (typeof obj !== "object" || obj === null) {
return obj;
}
if (Array.isArray(obj)) {
return obj.map(sortObjectByKey);
}
const sortedKeys = Object.keys(obj).sort();
const result: Record<string, any> = {};
sortedKeys.forEach((key) => {
result[key] = sortObjectByKey(obj[key]);
});
return result;
}
export function sortedJsonByKeyStringify(obj: Record<string, any>): string {
return JSON.stringify(sortObjectByKey(obj));
}