-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay 6 - SortByLength.js
46 lines (33 loc) · 973 Bytes
/
Day 6 - SortByLength.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*Sort By Length
https://scrimba.com/scrim/coe6b4796bcba3acea421b7e7
DESCRIPTION:
Given an array of strings, sort them in the order of increasing lengths.
If two strings have them same length, their relative order must be the same as in the initial array.
Example:
For
inputArray = ["abc", "", "a", "zz"]
the output should be
sortByLength(inputArray) = ["", "a", "zz", "abc", "aaa"]
Hints: sort()
*/
const sortByLength = (strs) => {
const compare = (a, b) => {
return a.length < b.length ? -1 : 1
}
return strs.sort(compare)
}
/**
* 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"]);
});
});