Skip to content

Commit

Permalink
sorting the flattened account structure by name
Browse files Browse the repository at this point in the history
  • Loading branch information
alensiljak committed Sep 7, 2023
1 parent 2697b18 commit c94304c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
5 changes: 4 additions & 1 deletion src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,11 @@ impl Account {
fn flatten<'a>(&'a self, nodes: &mut Vec<&'a Account>) {
// Push the current node to the Vec
nodes.push(self);
//
let mut children: Vec<&Account> = self.accounts.values().into_iter().collect();
children.sort_unstable_by_key(|acc| &acc.name);
// If the node has children, recursively call flatten on them
for (_name, child) in &self.accounts {
for child in children {
child.flatten(nodes);
}
}
Expand Down
11 changes: 6 additions & 5 deletions tests/accounts-tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ fn test_creating_account_tree() {
// Assert
let accounts = journal.master.flatten_account_tree();
assert_eq!(5, accounts.len());
assert_eq!("", accounts.iter().nth(0).unwrap().name);
assert_eq!("Expenses", accounts.iter().nth(1).unwrap().name);
assert_eq!("Food", accounts.iter().nth(2).unwrap().name);
assert_eq!("Assets", accounts.iter().nth(3).unwrap().name);
assert_eq!("Cash", accounts.iter().nth(4).unwrap().name);
let mut iterator = accounts.iter();
assert_eq!("", iterator.next().unwrap().name);
assert_eq!("Assets", iterator.next().unwrap().name);
assert_eq!("Cash", iterator.next().unwrap().name);
assert_eq!("Expenses", iterator.next().unwrap().name);
assert_eq!("Food", iterator.next().unwrap().name);
}

0 comments on commit c94304c

Please sign in to comment.