/// <summary> /// TODO: Implement as behavior /// </summary> /// <param name="shape"></param> /// <returns></returns> private static IEnumerable<dynamic> ordered_hack(dynamic shape) { IEnumerable<dynamic> unordered = shape; if (unordered == null || unordered.Count() < 2) return shape; var i = 1; var progress = 1; var flatPositionComparer = new FlatPositionComparer(); var ordering = unordered.Select(item => { var position = (item == null || item.GetType().GetProperty("Metadata") == null || item.Metadata.GetType().GetProperty("Position") == null) ? null : item.Metadata.Position; return new { item, position }; }).ToList(); // since this isn't sticking around (hence, the "hack" in the name), throwing (in) a gnome while (i < ordering.Count()) { if (flatPositionComparer.Compare(ordering[i].position, ordering[i - 1].position) > -1) { if (i == progress) progress = ++i; else i = progress; } else { var higherThanItShouldBe = ordering[i]; ordering[i] = ordering[i - 1]; ordering[i - 1] = higherThanItShouldBe; if (i > 1) --i; } } return ordering.Select(ordered => ordered.item).ToList(); }
private static IEnumerable <MenuItem> Merge(IEnumerable <IEnumerable <MenuItem> > sources) { var comparer = new MenuItemComparer(); var orderer = new FlatPositionComparer(); return(sources.SelectMany(x => x).ToArray() // group same menus .GroupBy(key => key, (key, items) => Join(items), comparer) // group same position .GroupBy(item => item.Position) // order position groups by position .OrderBy(positionGroup => positionGroup.Key, orderer) // ordered by item text in the postion group .SelectMany(positionGroup => positionGroup.OrderBy(item => item.Text == null ? "" : item.Text.TextHint))); }
public static IEnumerable <dynamic> Order(dynamic shape) { IEnumerable <dynamic> unordered = shape; if (unordered == null || unordered.Count() < 2) { return(shape); } var i = 1; var progress = 1; var flatPositionComparer = new FlatPositionComparer(); var ordering = unordered.Select(item => { var position = (item == null || item.GetType().GetProperty("Metadata") == null || item.Metadata.GetType().GetProperty("Position") == null) ? null : item.Metadata.Position; return(new { item, position }); }).ToList(); // since this isn't sticking around (hence, the "hack" in the name), throwing (in) a gnome while (i < ordering.Count()) { if (flatPositionComparer.Compare(ordering[i].position, ordering[i - 1].position) > -1) { if (i == progress) { progress = ++i; } else { i = progress; } } else { var higherThanItShouldBe = ordering[i]; ordering[i] = ordering[i - 1]; ordering[i - 1] = higherThanItShouldBe; if (i > 1) { --i; } } } return(ordering.Select(ordered => ordered.item).ToList()); }
private IEnumerable <Group> BuildGroups(string partitions, IEnumerable <ZoneEntry> zones) { var partitionCodes = (":before " + partitions + " :* :after").Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); var itemsRemaining = zones.SelectMany(zone => zone.Items.Where(x => x.WasExecuted == false)); Group catchAllItem = null; var positionComparer = new FlatPositionComparer(); var results = new List <Group>(); foreach (var code in partitionCodes) { if (code == ":*") { catchAllItem = new Group(); results.Add(catchAllItem); } else { var value = code; var items = itemsRemaining .Where(x => (":" + x.Position).StartsWith(value)) .OrderBy(x => x.Position, positionComparer); results.Add(new Group { Items = items.ToArray() }); itemsRemaining = itemsRemaining.Except(items).ToArray(); } } if (catchAllItem != null) { catchAllItem.Items = itemsRemaining .OrderBy(x => x.Position, positionComparer) .ToArray(); } return(results); }
private static string SelectBestPositionValue(IEnumerable<string> positions) { var comparer = new FlatPositionComparer(); return positions.Aggregate(string.Empty, (agg, pos) => string.IsNullOrEmpty(agg) ? pos : string.IsNullOrEmpty(pos) ? agg : comparer.Compare(agg, pos) < 0 ? agg : pos); }
private static IEnumerable<MenuItem> Merge(IEnumerable<IEnumerable<MenuItem>> sources) { var comparer = new MenuItemComparer(); var orderer = new FlatPositionComparer(); return sources.SelectMany(x => x).ToArray() // group same menus .GroupBy(key => key, (key, items) => Join(items), comparer) // group same position .GroupBy(item => item.Position) // order position groups by position .OrderBy(positionGroup => positionGroup.Key, orderer) // ordered by item text in the postion group .SelectMany(positionGroup => positionGroup.OrderBy(item => item.Text == null ? "" : item.Text.TextHint)); }
public static IEnumerable<dynamic> Order(dynamic shape) { IEnumerable<dynamic> unordered = shape; if (unordered == null || unordered.Count() < 2) return shape; var i = 1; var progress = 1; var flatPositionComparer = new FlatPositionComparer(); var ordering = unordered.Select(item => { string position = null; var itemPosition = item as IPositioned; if (itemPosition != null) { position = itemPosition.Position; } return new { item, position }; }).ToList(); // since this isn't sticking around (hence, the "hack" in the name), throwing (in) a gnome while (i < ordering.Count()) { if (flatPositionComparer.Compare(ordering[i].position, ordering[i - 1].position) > -1) { if (i == progress) progress = ++i; else i = progress; } else { var higherThanItShouldBe = ordering[i]; ordering[i] = ordering[i - 1]; ordering[i - 1] = higherThanItShouldBe; if (i > 1) --i; } } return ordering.Select(ordered => ordered.item).ToList(); }
private static IEnumerable<MenuItem> Merge(IEnumerable<IEnumerable<MenuItem>> sources) { var comparer = new MenuItemComparer(); var orderer = new FlatPositionComparer(); return sources.SelectMany(x => x).ToArray() //相同的菜单 .GroupBy(key => key, (key, items) => Join(items), comparer) //相同的位置 .GroupBy(item => item.Position) .OrderBy(positionGroup => positionGroup.Key, orderer) .SelectMany(positionGroup => positionGroup.OrderBy(item => item.Text == null ? string.Empty : item.Text.TextHint)); }