<!-- GENERATED FILE - DO NOT EDIT This file was generated by [MarkdownSnippets](https://github.com/SimonCropp/MarkdownSnippets). Source File: /docs/mdsource/ordering.source.md To change this file edit the source file and then run MarkdownSnippets. --> # Ordering ## Order properties alphabetically Serialized properties can optionally be ordering alphabetically, ie ignoring the order they are defined when using reflection. <!-- snippet: OrderProperties --> <a id='snippet-OrderProperties'></a> ```cs public static class ModuleInitializer { [ModuleInitializer] public static void Init() => VerifierSettings.SortPropertiesAlphabetically(); } ``` <sup><a href='/src/ModuleInitDocs/OrderProperties.cs#L3-L12' title='Snippet source file'>snippet source</a> | <a href='#snippet-OrderProperties' title='Start of snippet'>anchor</a></sup> <!-- endSnippet --> ## Dictionary order Dictionaries are ordering by key. To disable use: <!-- snippet: DontOrderDictionaries --> <a id='snippet-DontOrderDictionaries'></a> ```cs [Fact] public Task DontOrderDictionaries() { var dictionary = new Dictionary<string, string> { { "Entry_1", "1234" }, { "Entry_3", "1234" }, { "Entry_2", "5678" } }; return Verify(dictionary) .DontSortDictionaries(); } ``` <sup><a href='/src/Verify.Tests/Serialization/OrderTests.cs#L172-L194' title='Snippet source file'>snippet source</a> | <a href='#snippet-DontOrderDictionaries' title='Start of snippet'>anchor</a></sup> <a id='snippet-DontOrderDictionaries-1'></a> ```cs [Fact] public Task DontOrderDictionaries() { var dictionary = new Dictionary<string, string> { { "Entry_1", "1234" }, { "Entry_3", "1234" }, { "Entry_2", "5678" } }; return Verify(dictionary) .DontSortDictionaries(); } ``` <sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L146-L168' title='Snippet source file'>snippet source</a> | <a href='#snippet-DontOrderDictionaries-1' title='Start of snippet'>anchor</a></sup> <!-- endSnippet --> ## Json/JObject ordered Json and JObject are not ordered. To enable ordering use: <!-- snippet: OrderJsonObjects --> <a id='snippet-OrderJsonObjects'></a> ```cs public static class ModuleInitializer { [ModuleInitializer] public static void Init() => VerifierSettings.SortJsonObjects(); } ``` <sup><a href='/src/ModuleInitDocs/OrderJson.cs#L3-L12' title='Snippet source file'>snippet source</a> | <a href='#snippet-OrderJsonObjects' title='Start of snippet'>anchor</a></sup> <!-- endSnippet --> ## Ordering IEnumerable items Items in an instance of an IEnumerable can be ordered. This is helpful when verifying items that can have an inconsistent order, for example reading items from a database. ### OrderEnumerableBy #### Globally <!-- snippet: OrderEnumerableByGlobal --> <a id='snippet-OrderEnumerableByGlobal'></a> ```cs [ModuleInitializer] public static void OrderEnumerableByInitializer() => VerifierSettings.OrderEnumerableBy<TargetForGlobal>(_ => _.Value); ``` <sup><a href='/src/Verify.Tests/Serialization/OrderTests.cs#L3-L9' title='Snippet source file'>snippet source</a> | <a href='#snippet-OrderEnumerableByGlobal' title='Start of snippet'>anchor</a></sup> <!-- endSnippet --> #### Instance <!-- snippet: OrderEnumerableBy --> <a id='snippet-OrderEnumerableBy'></a> ```cs [Fact] public Task EnumerableOrder() { var settings = new VerifySettings(); settings.OrderEnumerableBy<Target>(_ => _.Value); return Verify( new List<Target> { new("a"), new("c"), new("b") }, settings); } ``` <sup><a href='/src/Verify.Tests/Serialization/OrderTests.cs#L45-L62' title='Snippet source file'>snippet source</a> | <a href='#snippet-OrderEnumerableBy' title='Start of snippet'>anchor</a></sup> <!-- endSnippet --> #### Fluent <!-- snippet: OrderEnumerableByFluent --> <a id='snippet-OrderEnumerableByFluent'></a> ```cs [Fact] public Task EnumerableOrderFluent() => Verify( new List<Target> { new("a"), new("c"), new("b") }) .OrderEnumerableBy<Target>(_ => _.Value); ``` <sup><a href='/src/Verify.Tests/Serialization/OrderTests.cs#L98-L111' title='Snippet source file'>snippet source</a> | <a href='#snippet-OrderEnumerableByFluent' title='Start of snippet'>anchor</a></sup> <!-- endSnippet --> #### Result The resulting file will be: <!-- snippet: OrderTests.EnumerableOrder.verified.txt --> <a id='snippet-OrderTests.EnumerableOrder.verified.txt'></a> ```txt [ { Value: a }, { Value: b }, { Value: c } ] ``` <sup><a href='/src/Verify.Tests/Serialization/OrderTests.EnumerableOrder.verified.txt#L1-L11' title='Snippet source file'>snippet source</a> | <a href='#snippet-OrderTests.EnumerableOrder.verified.txt' title='Start of snippet'>anchor</a></sup> <!-- endSnippet --> ### OrderEnumerableByDescending #### Globally <!-- snippet: OrderEnumerableByDescendingGlobal --> <a id='snippet-OrderEnumerableByDescendingGlobal'></a> ```cs [ModuleInitializer] public static void OrderEnumerableByDescendingInitializer() => VerifierSettings.OrderEnumerableByDescending<TargetForGlobalDescending>(_ => _.Value); ``` <sup><a href='/src/Verify.Tests/Serialization/OrderTests.cs#L23-L29' title='Snippet source file'>snippet source</a> | <a href='#snippet-OrderEnumerableByDescendingGlobal' title='Start of snippet'>anchor</a></sup> <!-- endSnippet --> #### Instance <!-- snippet: OrderEnumerableByDescending --> <a id='snippet-OrderEnumerableByDescending'></a> ```cs [Fact] public Task OrderEnumerableByDescending() { var settings = new VerifySettings(); settings.OrderEnumerableByDescending<Target>(_ => _.Value); return Verify( new List<Target> { new("a"), new("c"), new("b") }, settings); } ``` <sup><a href='/src/Verify.Tests/Serialization/OrderTests.cs#L79-L96' title='Snippet source file'>snippet source</a> | <a href='#snippet-OrderEnumerableByDescending' title='Start of snippet'>anchor</a></sup> <!-- endSnippet --> #### Fluent <!-- snippet: OrderEnumerableByDescendingFluent --> <a id='snippet-OrderEnumerableByDescendingFluent'></a> ```cs [Fact] public Task OrderEnumerableByDescendingFluent() => Verify( new List<Target> { new("a"), new("c"), new("b") }) .OrderEnumerableByDescending<Target>(_ => _.Value); ``` <sup><a href='/src/Verify.Tests/Serialization/OrderTests.cs#L113-L126' title='Snippet source file'>snippet source</a> | <a href='#snippet-OrderEnumerableByDescendingFluent' title='Start of snippet'>anchor</a></sup> <!-- endSnippet --> #### Result The resulting file will be: <!-- snippet: OrderTests.OrderEnumerableByDescending.verified.txt --> <a id='snippet-OrderTests.OrderEnumerableByDescending.verified.txt'></a> ```txt [ { Value: c }, { Value: b }, { Value: a } ] ``` <sup><a href='/src/Verify.Tests/Serialization/OrderTests.OrderEnumerableByDescending.verified.txt#L1-L11' title='Snippet source file'>snippet source</a> | <a href='#snippet-OrderTests.OrderEnumerableByDescending.verified.txt' title='Start of snippet'>anchor</a></sup> <!-- endSnippet -->