diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index ce10de11..3fc3cb78 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -1,4 +1,4 @@ - + @@ -51,7 +51,10 @@ + + + @@ -65,6 +68,10 @@ {3511528E-8696-40B8-85AB-97456347A497} csharp-algorithms + + {be3d2aa4-419a-42c4-9704-7fc62717ae2a} + linqsort + {A9022928-E9E9-45BE-BFEC-85A1A870A7EB} selectionsort @@ -80,4 +87,4 @@ - + \ No newline at end of file diff --git a/Tests/sort/ExtentionMethods.cs b/Tests/sort/ExtentionMethods.cs new file mode 100644 index 00000000..55edde7e --- /dev/null +++ b/Tests/sort/ExtentionMethods.cs @@ -0,0 +1,36 @@ +using System; +using System.Linq; +using csharp_algorithms; + +namespace Tests.sort +{ + internal static class ExtentionMethods + { + public static Node[] Copy(this Node[] array) + { + Node[] arrayCopy = new Node[array.Length]; + Array.Copy(array, arrayCopy, array.Length); + return arrayCopy; + } + + public static bool AreSameOrder(this Node[] a, Node[] b) + { + if (a == null|| b == null) return false; + if (a.Count() != b.Count()) return false; + + foreach(var node in a) + { + var indexOfNode = Array.IndexOf(a, node); + if (b[indexOfNode].Number == node.Number) + { + continue; + } + else + { + return false; + } + } + return a.Count() == b.Count(); + } + } +} \ No newline at end of file diff --git a/Tests/sort/LinqsortTests.cs b/Tests/sort/LinqsortTests.cs new file mode 100644 index 00000000..06a9b5da --- /dev/null +++ b/Tests/sort/LinqsortTests.cs @@ -0,0 +1,77 @@ +using csharp_algorithms; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using linqsort; + +namespace Tests.sort +{ + [TestClass] + public class LinqsortTests + { + private readonly LinqSort linqSort; + private readonly TestDataGenerator testDataGenerator; + + public LinqsortTests() + { + linqSort = new LinqSort(); + testDataGenerator = new TestDataGenerator(); + } + + [TestMethod] + public void OrderBy() + { + var testData = testDataGenerator.Generate(); + + var sorted = linqSort.OrderBy(testData.Copy()); + + Assert.AreNotEqual(testData, sorted); + } + + [TestMethod] + public void TypedSort() + { + var testData = testDataGenerator.Generate(); + + var sorted = linqSort.TypedSort(testData.Copy()); + + Assert.AreNotEqual(testData, sorted); + } + + [TestMethod] + public void GenericSort() + { + var testData = testDataGenerator.Generate(); + + var sorted = linqSort.GenericSort(testData.Copy()); + + Assert.AreNotEqual(testData, sorted); + } + + [TestMethod] + public void CheckSortOrdersAreTheSame() + { + var testData = testDataGenerator.Generate(); + + var genericSorted = linqSort.GenericSort(testData.Copy()); + var orderBySorted = linqSort.OrderBy(testData.Copy()); + var typedSorted = linqSort.TypedSort(testData.Copy()); + + Assert.IsTrue(genericSorted.AreSameOrder(orderBySorted)); + Assert.IsTrue(genericSorted.AreSameOrder(typedSorted)); + } + + [TestMethod] + public void CheckSortOrders() + { + var ordered = new Node[] { new Node(-999), new Node(10), new Node(15), new Node(88), new Node(99), new Node(569) }; + var unOrdered = new Node[] { new Node(10), new Node(88), new Node(-999), new Node(15), new Node(569), new Node(99) }; + + var genericSorted = linqSort.GenericSort(unOrdered.Copy()); + var orderBySorted = linqSort.OrderBy(unOrdered.Copy()); + var typedSorted = linqSort.TypedSort(unOrdered.Copy()); + + Assert.IsTrue(genericSorted.AreSameOrder(ordered)); + Assert.IsTrue(orderBySorted.AreSameOrder(ordered)); + Assert.IsTrue(typedSorted.AreSameOrder(ordered)); + } + } +} \ No newline at end of file diff --git a/Tests/sort/TestDataGenerator.cs b/Tests/sort/TestDataGenerator.cs new file mode 100644 index 00000000..f6b357ea --- /dev/null +++ b/Tests/sort/TestDataGenerator.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using csharp_algorithms; + +namespace Tests.sort +{ + class TestDataGenerator + { + private readonly int Min = int.MinValue; + private readonly int Max = int.MaxValue; + private readonly Random Random = new Random(); + + public Node[] Generate(int entries = 256) + { + if (entries <= 2) + { + return new Node[0]; + } + + var data = Enumerable + .Repeat(0, entries) + .Select(i => new Node(Random.Next(Min, Max))) + .ToArray(); + + return data; + } + } +} diff --git a/csharp-algorithms.sln b/csharp-algorithms.sln index 8cc34265..51bbb25c 100644 --- a/csharp-algorithms.sln +++ b/csharp-algorithms.sln @@ -17,6 +17,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "search", "search", "{F61757 EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "selectionsort", "selectionsort\selectionsort.csproj", "{A9022928-E9E9-45BE-BFEC-85A1A870A7EB}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "linqsort", "linqsort\linqsort\linqsort.csproj", "{BE3D2AA4-419A-42C4-9704-7FC62717AE2A}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -39,6 +41,10 @@ Global {A9022928-E9E9-45BE-BFEC-85A1A870A7EB}.Debug|Any CPU.Build.0 = Debug|Any CPU {A9022928-E9E9-45BE-BFEC-85A1A870A7EB}.Release|Any CPU.ActiveCfg = Release|Any CPU {A9022928-E9E9-45BE-BFEC-85A1A870A7EB}.Release|Any CPU.Build.0 = Release|Any CPU + {BE3D2AA4-419A-42C4-9704-7FC62717AE2A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BE3D2AA4-419A-42C4-9704-7FC62717AE2A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BE3D2AA4-419A-42C4-9704-7FC62717AE2A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BE3D2AA4-419A-42C4-9704-7FC62717AE2A}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -46,6 +52,7 @@ Global GlobalSection(NestedProjects) = preSolution {C557EF5B-AEFE-407B-8D99-43B9EA625B71} = {6A9C8607-AA57-4817-B2C8-B8DD5065AC7C} {A9022928-E9E9-45BE-BFEC-85A1A870A7EB} = {6A9C8607-AA57-4817-B2C8-B8DD5065AC7C} + {BE3D2AA4-419A-42C4-9704-7FC62717AE2A} = {6A9C8607-AA57-4817-B2C8-B8DD5065AC7C} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {7F2BFC66-A9D6-4C6D-A15A-984F23C11D29} diff --git a/csharp-algorithms/Node.cs b/csharp-algorithms/Node.cs index c9211548..1ed41c3a 100644 --- a/csharp-algorithms/Node.cs +++ b/csharp-algorithms/Node.cs @@ -1,6 +1,8 @@ -namespace csharp_algorithms +using System; + +namespace csharp_algorithms { - public class Node + public class Node : IComparable, IComparable { public int Number { get; set; } @@ -8,5 +10,17 @@ public Node(int number) { this.Number = number; } + + public int CompareTo(object obj) + { + return CompareTo((Node)obj); + } + + public int CompareTo(Node other) + { + if (Number > other.Number) return -1; + if (Number == other.Number) return 0; + return 1; + } } -} +} \ No newline at end of file diff --git a/linqsort/linqsort/LinqSort.cs b/linqsort/linqsort/LinqSort.cs new file mode 100644 index 00000000..bc51dd96 --- /dev/null +++ b/linqsort/linqsort/LinqSort.cs @@ -0,0 +1,63 @@ +using csharp_algorithms; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; + +namespace linqsort +{ + public class LinqSort + { + private readonly GenericComparer genericComparer; + private readonly TypedComparer typedComparer; + + public LinqSort() + { + genericComparer = new GenericComparer(); + typedComparer = new TypedComparer(); + } + + public Node[] OrderBy(Node[] dataToSort) + { + return dataToSort.OrderBy(o => o.Number).ToArray(); + } + + public Node[] GenericSort(Node[] dataToSort) + { + Array.Sort(dataToSort, genericComparer); + + return dataToSort; + } + + public Node[] TypedSort(Node[] dataToSort) + { + Array.Sort(dataToSort, typedComparer); + + return dataToSort; + } + + private class GenericComparer : IComparer + { + int IComparer.Compare(object x, object y) + { + return ((new CaseInsensitiveComparer()).Compare(y, x)); + } + } + + private class TypedComparer : IComparer + { + public int Compare(Node x, Node y) + { + if (x.Number > y.Number) + { + return 1; + } + if (x.Number < y.Number) + { + return -1; + } + return 0; + } + } + } +} \ No newline at end of file diff --git a/linqsort/linqsort/Properties/AssemblyInfo.cs b/linqsort/linqsort/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..79a4b31b --- /dev/null +++ b/linqsort/linqsort/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("linqsort")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("linqsort")] +[assembly: AssemblyCopyright("Copyright © 2018")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("be3d2aa4-419a-42c4-9704-7fc62717ae2a")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/linqsort/linqsort/linqsort.csproj b/linqsort/linqsort/linqsort.csproj new file mode 100644 index 00000000..5be33774 --- /dev/null +++ b/linqsort/linqsort/linqsort.csproj @@ -0,0 +1,60 @@ + + + + + Debug + AnyCPU + {BE3D2AA4-419A-42C4-9704-7FC62717AE2A} + Library + linqsort + linqsort + v4.6.1 + 512 + false + true + + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + {3511528e-8696-40b8-85ab-97456347a497} + csharp-algorithms + + + + \ No newline at end of file