function sortByLength(strs) {
// After much learning and refinement...
return strs.sort((a, b) => a.length - b.length);
/*
//custom sort function to compare the length of each item in the array
function(a,b){
if (a.length > b.length){ //if b is shorter, set its index lower
return 1;
}
if (a.length < b.length){//if b is higher, set a's index lower
return -1;
}
return 0; //keep them the same
}*/
}
/**
* 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"]);
});
});