public ArcCollection(bool isFsa, State <TData, TOffset> state) { _state = state; _isFsa = isFsa; _arcs = new List <Arc <TData, TOffset> >(); _arcComparer = ProjectionComparer <Arc <TData, TOffset> > .Create(arc => arc.PriorityType).Reverse(); }
private static IOrderedDataProducer <TSource> OrderBy <TSource, TKey>(IDataProducer <TSource> source, DotNet20.Func <TSource, TKey> selector, IComparer <TKey> comparer, bool descending) { source.ThrowIfNull("source"); comparer.ThrowIfNull("comparer"); IComparer <TSource> itemComparer = new ProjectionComparer <TSource, TKey>(selector, comparer); if (descending) { itemComparer = itemComparer.Reverse(); } // first, discard any existing "order by"s by going back to the producer IOrderedDataProducer <TSource> orderedProducer; bool first = true; while ((orderedProducer = source as IOrderedDataProducer <TSource>) != null) { if (first) { // keep the top-most comparer to enforce a balanced sort itemComparer = new LinkedComparer <TSource>(itemComparer, orderedProducer.Comparer); first = false; } source = orderedProducer.BaseProducer; } return(new OrderedDataProducer <TSource>(source, itemComparer)); }
static void Main() { // This uses lambda expressions and extension methods... IComparer <Product> comparer = ProjectionComparer <Product> .Create(p => p.Popularity).Reverse() .ThenBy(p => p.Price) .ThenBy(p => p.Name); // Products are sorted by popularity (highest first), price (lowest first) and then name Product remote = new Product(100, 25.00m, "Remote control"); Product mouse = new Product(50, 10.00m, "Wireless mouse"); Product headphones = new Product(100, 20.00m, "USB drive"); Product wipes = new Product(50, 10.00m, "Screen wipes"); Product game = new Product(150, 40.00m, "Shiny new game"); Product[] products = { remote, mouse, headphones, wipes, game }; Array.Sort(products, comparer); foreach (Product product in products) { Console.WriteLine(product); } Product otherGame = new Product(150, 40.00m, "Shiny new game"); Console.WriteLine("Two similar products compare as 0? {0}", comparer.Compare(game, otherGame) == 0); }
public void ThenByWithProjection() { var data = SampleType.SampleData; IComparer <SampleType> primary = ProjectionComparer <SampleType> .Create(t => t.First); data.Sort(primary.ThenBy(t => t.Second)); Assert.IsTrue(new[] { 2, 10, 5 }.SequenceEqual(data.Select(x => x.Second))); }
public void OrderByProjectionComparerNullComparer() { // (uses default when null) IComparer <int> comparer = new ProjectionComparer <int, int>(x => x, null).Reverse(); List <int> data = new List <int>(new[] { 2, 4, 1 }); data.Sort(comparer); Assert.AreEqual(data[0], 4); Assert.AreEqual(data[1], 2); Assert.AreEqual(data[2], 1); }
public static IOrderedEnumerable <TSource> OrderBy <TSource, TKey> ( this IEnumerable <TSource> source, Func <TSource, TKey> keySelector ) { return(new OrderedEnumerable <TSource> ( source, ProjectionComparer.Create(keySelector) )); }
private static IOrderedDataProducer <TSource> ThenBy <TSource, TKey>(IOrderedDataProducer <TSource> source, DotNet20.Func <TSource, TKey> selector, IComparer <TKey> comparer, bool descending) { comparer.ThrowIfNull("comparer"); IComparer <TSource> itemComparer = new ProjectionComparer <TSource, TKey>(selector, comparer); if (descending) { itemComparer = itemComparer.Reverse(); } itemComparer = new LinkedComparer <TSource>(source.Comparer, itemComparer); return(new OrderedDataProducer <TSource>(source, itemComparer)); }
public static IOrderedEnumerable <TSource> OrderByDescending <TSource, TKey> ( this IEnumerable <TSource> source, Func <TSource, TKey> keySelector ) { return(new OrderedEnumerable <TSource> ( source, new ReverseComparer <TSource>(ProjectionComparer.Create(keySelector)) )); }
public void HandleEventInstance(ProjectionValueReturned eventInstance) { if (_requestedProjections.Contains(eventInstance, _comparer)) { ProjectionComparer compareTo = new ProjectionComparer(eventInstance); int pos = _requestedProjections.FindIndex(0, compareTo.Equals); if (pos >= 0) { _requestedProjections.RemoveAt(pos); } } }
public void Equals_DifferentValues_False() { var comparer = ProjectionComparer <Foo> .Create(f => new { f.Bar, f.Baz }); Assert.IsFalse(comparer.Equals( new Foo { Bar = "foo", Baz = 2, Qux = DateTime.Now }, new Foo { Bar = "foo", Baz = 3, Qux = DateTime.Now.AddHours(-1) }) ); }
public void ExplicitComparerTest() { // a is greater than Z with an ordinal comparison, but not with a case-insensitive ordinal comparison NameAndNumber lowerA = new NameAndNumber { Name = "a", Number = 10 }; NameAndNumber upperZ = new NameAndNumber { Name = "Z", Number = 10 }; IComparer<NameAndNumber> ordinalComparer = new ProjectionComparer<NameAndNumber, string> (z => z.Name, StringComparer.Ordinal); IComparer<NameAndNumber> insensitiveComparer = new ProjectionComparer<NameAndNumber, string> (z => z.Name, StringComparer.OrdinalIgnoreCase); Assert.Greater(ordinalComparer.Compare(lowerA, upperZ), 0); Assert.Less(insensitiveComparer.Compare(lowerA, upperZ), 0); }
private void LoadCollectionView() { var vm = (WordListsViewModel)DataContext; if (vm == null) { return; } WordListsGrid.CurrentColumn = null; WordListsGrid.CurrentItem = null; WordListsGrid.Columns.Clear(); var view = new DataGridCollectionView(vm.Varieties, typeof(WordListsVarietyViewModel), false, false); view.ItemProperties.Add(new DataGridItemProperty("Variety", ".", typeof(WordListsVarietyViewModel))); IComparer sortComparer = ProjectionComparer <WordListsVarietyMeaningViewModel> .Create(meaning => meaning.StrRep); for (int i = 0; i < vm.Meanings.Count; i++) { view.ItemProperties.Add(new DataGridItemProperty("Meaning" + i, $"Meanings[{i}]", typeof(WordListsVarietyMeaningViewModel)) { SortComparer = sortComparer }); } vm.VarietiesView = view; WordListsGrid.Items.SortDescriptions.Clear(); var headerColumn = new Column { FieldName = "Variety" }; DataGridControlBehaviors.SetIsRowHeader(headerColumn, true); DataGridControlBehaviors.SetAutoSize(headerColumn, true); DataGridControlBehaviors.SetAutoSizePadding(headerColumn, 18); WordListsGrid.Columns.Add(headerColumn); for (int i = 0; i < vm.Meanings.Count; i++) { var column = new Column { FieldName = "Meaning" + i, Width = 100, CellEditor = WordListsGrid.DefaultCellEditors[typeof(WordListsVarietyMeaningViewModel)] }; var titleBinding = new Binding($"DataGridControl.DataContext.Meanings[{i}].Gloss") { RelativeSource = RelativeSource.Self }; BindingOperations.SetBinding(column, ColumnBase.TitleProperty, titleBinding); WordListsGrid.Columns.Add(column); } }
/// <summary> /// Sorts the elements in the entire System.Collections.Generic.List{T} using /// a projection. /// </summary> /// <param name="source">Data source</param> /// <param name="selector">The projection to use to obtain values for comparison</param> /// <param name="comparer">The comparer to use to compare projected values (on null to use the default comparer)</param> /// <param name="descending">Should the list be sorted ascending or descending?</param> public static void Sort <T, TValue>(this List <T> source, DotNet20.Func <T, TValue> selector, IComparer <TValue> comparer, bool descending) { if (source == null) { throw new ArgumentNullException("source"); } if (comparer == null) { comparer = Comparer <TValue> .Default; } IComparer <T> itemComparer = new ProjectionComparer <T, TValue>(selector, comparer); if (descending) { itemComparer = itemComparer.Reverse(); } source.Sort(itemComparer); }
public void ExplicitComparerTest() { // a is greater than Z with an ordinal comparison, but not with a case-insensitive ordinal comparison NameAndNumber lowerA = new NameAndNumber { Name = "a", Number = 10 }; NameAndNumber upperZ = new NameAndNumber { Name = "Z", Number = 10 }; IComparer <NameAndNumber> ordinalComparer = new ProjectionComparer <NameAndNumber, string> (z => z.Name, StringComparer.Ordinal); IComparer <NameAndNumber> insensitiveComparer = new ProjectionComparer <NameAndNumber, string> (z => z.Name, StringComparer.OrdinalIgnoreCase); Assert.Greater(ordinalComparer.Compare(lowerA, upperZ), 0); Assert.Less(insensitiveComparer.Compare(lowerA, upperZ), 0); }
/// <summary> /// Sorts the elements in the entire System.Collections.Generic.List{T} using /// a projection. /// </summary> /// <param name="source">Data source</param> /// <param name="selector">The projection to use to obtain values for comparison</param> /// <param name="comparer">The comparer to use to compare projected values (on null to use the default comparer)</param> /// <param name="descending">Should the list be sorted ascending or descending?</param> public static void Sort <T, TValue>(this List <T> source, Func <T, TValue> selector, IComparer <TValue> comparer, bool descending) { switch (source) { case null: throw new ArgumentNullException("source"); } comparer = comparer switch { null => Comparer <TValue> .Default, _ => comparer }; IComparer <T> itemComparer = new ProjectionComparer <T, TValue>(selector, comparer); switch (descending) { case true: itemComparer = itemComparer.Reverse(); break; } source.Sort(itemComparer); }
public void Sort <TKey>(Func <T, TKey> keySelector) { var comparer = ProjectionComparer <T> .Create(keySelector); Sort(comparer); }
private void LoadCollectionView() { var vm = (MultipleWordAlignmentViewModel)DataContext; AlignmentGrid.Columns.Clear(); var view = new DataGridCollectionView(vm.Words, typeof(MultipleWordAlignmentWordViewModel), false, false); view.ItemProperties.Add(new DataGridItemProperty("Variety", "Variety", typeof(VarietyViewModel)) { SortComparer = ProjectionComparer <VarietyViewModel> .Create(v => v.Name) }); view.ItemProperties.Add(new DataGridItemProperty("StrRep", "StrRep", typeof(string))); view.ItemProperties.Add(new DataGridItemProperty("CognateSetIndex", "CognateSetIndex", typeof(int))); view.ItemProperties.Add(new DataGridItemProperty("Prefix", "Prefix", typeof(string))); for (int i = 0; i < vm.ColumnCount; i++) { view.ItemProperties.Add(new DataGridItemProperty("Column" + i, string.Format("Columns[{0}]", i), typeof(string))); } view.ItemProperties.Add(new DataGridItemProperty("Suffix", "Suffix", typeof(string))); if (vm.GroupByCognateSet) { Debug.Assert(view.GroupDescriptions != null); view.GroupDescriptions.Add(new DataGridGroupDescription("CognateSetIndex")); } vm.WordsView = view; var headerColumn = new Column { FieldName = "Variety" }; DataGridControlBehaviors.SetIsRowHeader(headerColumn, true); DataGridControlBehaviors.SetAutoSize(headerColumn, true); DataGridControlBehaviors.SetAutoSizePadding(headerColumn, 18); AlignmentGrid.Columns.Add(headerColumn); object fontSizeObj = System.Windows.Application.Current.FindResource("PhoneticFontSize"); Debug.Assert(fontSizeObj != null); var fontSize = (double)fontSizeObj; var prefixColumn = new Column { FieldName = "Prefix", ReadOnly = true, CanBeCurrentWhenReadOnly = false }; DataGridControlBehaviors.SetAutoSize(prefixColumn, true); DataGridControlBehaviors.SetAutoSizePadding(prefixColumn, 9); DataGridControlBehaviors.SetFontSizeHint(prefixColumn, fontSize); AlignmentGrid.Columns.Add(prefixColumn); for (int i = 0; i < vm.ColumnCount; i++) { var column = new Column { FieldName = "Column" + i }; DataGridControlBehaviors.SetAutoSize(column, true); DataGridControlBehaviors.SetAutoSizePadding(column, 9); DataGridControlBehaviors.SetFontSizeHint(column, fontSize); AlignmentGrid.Columns.Add(column); } var suffixColumn = new Column { FieldName = "Suffix", ReadOnly = true, CanBeCurrentWhenReadOnly = false }; DataGridControlBehaviors.SetAutoSize(suffixColumn, true); DataGridControlBehaviors.SetAutoSizePadding(suffixColumn, 9); DataGridControlBehaviors.SetFontSizeHint(suffixColumn, fontSize); AlignmentGrid.Columns.Add(suffixColumn); AlignmentGrid.CurrentItem = null; }
public static void Sort <TSource, TKey>(IList <TSource> arr, Func <TSource, TKey> keySelector) { var comparer = ProjectionComparer <TSource> .Create(keySelector); Sort(arr, comparer); }
public void ProjectToNumberWithIgnoredParameter() { IComparer <NameAndNumber> comparer = ProjectionComparer.Create(A10, x => x.Number); TestComparisons(comparer); }
public void ProjectToNumberWithGenericType() { IComparer <NameAndNumber> comparer = ProjectionComparer <NameAndNumber> .Create(x => x.Number); TestComparisons(comparer); }
public void ProjectToNumberWithExplicitType() { IComparer <NameAndNumber> comparer = ProjectionComparer.Create((NameAndNumber x) => x.Number); TestComparisons(comparer); }