function sortByLength(strs) {
// With method
//return strs.sort((stringOne, stringTwo) => stringOne.length - stringTwo.length);
// With insertion sort
for(let i = 1; i < strs.length; i++) {
let j = i;
let toInsert = strs[i];
while((j > 0) && (strs[j - 1].length > toInsert.length)) {
strs[j] = strs[j-1];
j--;
}
strs[j] = toInsert;
}
return strs;
}
/**
* Test Suite
*/
describe('sortByLength()', () => {
it('sorts the strings from shortest to longest', () => {
// arrange
const strs = ["abc", "", "aaa", "a", "zz"];
// act
const result = sortByLength(strs);
// log
console.log("result: ", result);
// assert
expect(result).toEqual(["", "a", "zz", "abc", "aaa"]);
});
});