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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use std::collections::HashMap;
use std::fmt::Write;
use std::path::Path;

///
/// It reads the input as lines and parses each one as a path and then sorts them
/// out by the length of their path components.
/// Pass invert as true to invert the order
///
pub fn sort_path_length(input: String, invert: bool) -> String {
    let mut path_hash_map: HashMap<usize, Vec<&str>> = HashMap::new();

    for line_path in input.lines() {
        let components = Path::new(line_path).components().collect::<Vec<_>>();
        path_hash_map
            .entry(components.len())
            .and_modify(|v| v.push(line_path))
            .or_insert(vec![line_path]);
    }

    let mut lengths = path_hash_map.keys().collect::<Vec<_>>();

    if invert {
        lengths.sort_by(|a, b| b.partial_cmp(a).unwrap());
    } else {
        lengths.sort();
    }

    let mut output: Vec<&str> = Vec::new();

    for k in lengths {
        match path_hash_map.get(k) {
            Some(p) => {
                let mut paths = p.clone();

                if invert {
                    paths.sort_by(|a, b| b.partial_cmp(a).unwrap());
                } else {
                    paths.sort();
                }

                output.append(&mut paths);
            }
            None => {
                // noop
            }
        };
    }

    output.iter().fold(String::new(), |mut o, p| {
        o.write_str(p).unwrap();
        o.write_char('\n').unwrap();
        o
    })
}

#[test]
pub fn test_default_sort_order() {
    let input = "/a/absolute/path\n/a/b/c/d/e\n/a\n/a/dpasodj";

    let output = sort_path_length(input.to_string(), false);

    let expected = "/a\n/a/dpasodj\n/a/absolute/path\n/a/b/c/d/e\n";

    assert_eq!(expected, output);
}

#[test]
pub fn test_inverted_sort_order() {
    let input = "/a/absolute/path\n/a/b/c/d/e\n/a\n/a/dpasodj";

    let output = sort_path_length(input.to_string(), true);

    let expected = "/a/b/c/d/e\n/a/absolute/path\n/a/dpasodj\n/a\n";

    assert_eq!(expected, output);
}