/** * Iteration primitives. `map_sorted_amounts' allows one to visit * each amount in balance in the proper order for displaying to the * user. Mostly used by `print' and other routinse where the sort * order of the amounts' commodities is significant. */ public void MapSortedAmounts(Action <Amount> fn) { if (Amounts.Any()) { if (Amounts.Count == 1) { Amount amount = Amounts.First().Value; if ((bool)amount) { fn(amount); } } else { List <Amount> sorted = SortedAmounts(); sorted.ForEach(amt => fn(amt)); } } }
/** * Iteration primitives. `map_sorted_amounts' allows one to visit * each amount in balance in the proper order for displaying to the * user. Mostly used by `print' and other routinse where the sort * order of the amounts' commodities is significant. */ public void MapSortedAmounts(Action <Amount> fn) { if (Amounts.Any()) { if (Amounts.Count == 1) { Amount amount = Amounts.First().Value; if ((bool)amount) { fn(amount); } } else { List <Amount> sorted = Amounts.Values. Where(amt => (bool)amt). // [DM] Enumerable.OrderBy is a stable sort that preserve original positions for equal items OrderBy(amt => amt, new BalanceCompareByCommodityComparison()). ToList(); sorted.ForEach(amt => fn(amt)); } } }