private void MakeHierarchy(IHierarchicalInfoAnalyser[] analysers, bool[] orders, CardViewModel card) { HierarchicalResultViewModel current = _buildingRoot; for (int index = 0; index <= analysers.Length; index++) { IList <HierarchicalResultViewModel> children = current.Children; HierarchicalResultViewModel next = null; IComparable value = index < analysers.Length ? analysers[index].Analyse(card) : card.Name; bool isAscendentOrder = index >= orders.Length || orders[index]; int i; for (i = 0; i < children.Count; i++) { int result = value.CompareTo(children[i].Value); if (result == 0) { next = children[i]; break; } if ((result < 0) == isAscendentOrder) { break; } } if (next == null) { next = index < analysers.Length ? new HierarchicalResultViewModel(value) : new HierarchicalResultNodeViewModel(value); current.Children.Insert(i, next); } if (index == analysers.Length) { // ReSharper disable PossibleNullReferenceException (next as HierarchicalResultNodeViewModel).AddCard(card); // ReSharper restore PossibleNullReferenceException } current = next; } }
private HierarchicalResultNodeViewModel FindBestName(HierarchicalResultViewModel toInspect, CardViewModel saveSelected) { if (toInspect == null || saveSelected == null) { return(null); } HierarchicalResultNodeViewModel res = null; HierarchicalResultNodeViewModel nodevm = toInspect as HierarchicalResultNodeViewModel; Matching resMatch = FindBestMatch(nodevm, saveSelected); if (resMatch == Matching.Full) { return(nodevm); } if (resMatch == Matching.Name) { res = nodevm; } foreach (HierarchicalResultViewModel child in toInspect.Children) { nodevm = FindBestName(child, saveSelected); resMatch = FindBestMatch(nodevm, saveSelected); if (resMatch == Matching.Full) { return(nodevm); } if (resMatch == Matching.Name && res == null) { res = nodevm; } } return(res); }