Skip to content

Instantly share code, notes, and snippets.

@IvanAdmaers
Last active May 17, 2022 14:15
Show Gist options
  • Save IvanAdmaers/c42cfc26f55a110b416fa4e53ff7c414 to your computer and use it in GitHub Desktop.
Save IvanAdmaers/c42cfc26f55a110b416fa4e53ff7c414 to your computer and use it in GitHub Desktop.
Sort object | Sort the keys of an object recursively
import sortObject from './sortObject';
describe('sortObject', () => {
it('should sort one-dimensional object', () => {
const input = { c: 3, b: '2', a: null };
const output = { a: null, b: '2', c: 3 };
expect(sortObject(input)).toEqual(output);
});
it('should sort two-dimensional object', () => {
const input = { c: 3, a: { b2: 'b2', a2: 'a2' }, b: '2' };
const output = { a: { a2: 'a2', b2: 'b2' }, b: '2', c: 3 };
expect(sortObject(input)).toEqual(output);
});
it('should sort three-dimensional object', () => {
const input = {
c: 3,
a: { b2: 'b2', a2: 'a2', c2: { b3: 'b3', a3: 'a3' } },
b: null,
};
const output = {
a: { a2: 'a2', b2: 'b2', c2: { a3: 'a3', b3: 'b3' } },
b: null,
c: 3,
};
expect(sortObject(input)).toEqual(output);
});
it('should sort an array of strings in object', () => {
const input = { arr: ['a', 'c', 'b'] };
const output = { arr: ['a', 'b', 'c'] };
expect(sortObject(input)).toEqual(output);
});
it('should sort an array of numbers in object', () => {
const input = { arr: [1, 3, 2] };
const output = { arr: [1, 2, 3] };
expect(sortObject(input)).toEqual(output);
});
it('should sort an array of strings, numbers and objects in object', () => {
const input = { arr: [1, { b: 'b', a: 'a' }, 2, 'lol'] };
const output = { arr: [1, 2, { a: 'a', b: 'b' }, 'lol'] };
expect(sortObject(input)).toEqual(output);
});
it('should sort correct when there are non-primitives with primitives', () => {
const input = { arr: [{ a: [{}, 'a'] }] };
const wrongOutput = { arr: [{ a: ['a', {}] }] };
expect(sortObject(input)).not.toEqual(wrongOutput);
});
});
/**
* This function sorts object keys alphabetical and arrays in object
* { b: 2, a: 1 } => { a: 1, b: 2 }
* { c: { e: 2, d: 1 } } => { c: { d: 1, e: 2 } }
* { f: ['h', 'g'] } => { f: ['g', 'h'] }
* { i: [{ k: 2, j: 1 }] } => { i: [{ j: 1, k: 2 }] }
*/
const sortObject = <InputObjectType>(
inputObject: InputObjectType
): InputObjectType => {
const result = {};
const keys: Array<string> = Object.keys(inputObject);
const sortedKeys: Array<string> = keys.sort();
sortedKeys.forEach((key: string): void => {
const value = inputObject[key];
if (typeof value === 'object' && !Array.isArray(value) && value !== null) {
result[key] = sortObject(value);
return;
}
if (Array.isArray(value)) {
const resultArray: Array<
string | number | undefined | null | object | boolean | symbol | bigint
> = [];
const sortedArray = value.sort();
sortedArray.forEach((arrayItem) => {
if (typeof arrayItem !== 'object' && arrayItem !== null) {
resultArray.push(arrayItem);
return;
}
const sortedObject = sortObject(arrayItem);
resultArray.push(sortedObject);
});
result[key] = resultArray;
return;
}
result[key] = value;
});
return result as InputObjectType;
};
export default sortObject;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment