Skip to content

Adding sortings via LINQ #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions Tests/Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.props" Condition="Exists('..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
Expand Down Expand Up @@ -51,7 +51,10 @@
<ItemGroup>
<Compile Include="sort\BubblesortTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="sort\ExtentionMethods.cs" />
<Compile Include="sort\LinqsortTests.cs" />
<Compile Include="sort\SelectionsortTests.cs" />
<Compile Include="sort\TestDataGenerator.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand All @@ -65,6 +68,10 @@
<Project>{3511528E-8696-40B8-85AB-97456347A497}</Project>
<Name>csharp-algorithms</Name>
</ProjectReference>
<ProjectReference Include="..\linqsort\linqsort\linqsort.csproj">
<Project>{be3d2aa4-419a-42c4-9704-7fc62717ae2a}</Project>
<Name>linqsort</Name>
</ProjectReference>
<ProjectReference Include="..\selectionsort\selectionsort.csproj">
<Project>{A9022928-E9E9-45BE-BFEC-85A1A870A7EB}</Project>
<Name>selectionsort</Name>
Expand All @@ -80,4 +87,4 @@
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.targets'))" />
</Target>
<Import Project="..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.targets" Condition="Exists('..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.targets')" />
</Project>
</Project>
36 changes: 36 additions & 0 deletions Tests/sort/ExtentionMethods.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
77 changes: 77 additions & 0 deletions Tests/sort/LinqsortTests.cs
Original file line number Diff line number Diff line change
@@ -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));
}
}
}
29 changes: 29 additions & 0 deletions Tests/sort/TestDataGenerator.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
7 changes: 7 additions & 0 deletions csharp-algorithms.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -39,13 +41,18 @@ 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
EndGlobalSection
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}
Expand Down
20 changes: 17 additions & 3 deletions csharp-algorithms/Node.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
namespace csharp_algorithms
using System;

namespace csharp_algorithms
{
public class Node
public class Node : IComparable, IComparable<Node>
{
public int Number { get; set; }

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;
}
}
}
}
63 changes: 63 additions & 0 deletions linqsort/linqsort/LinqSort.cs
Original file line number Diff line number Diff line change
@@ -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<Node>
{
public int Compare(Node x, Node y)
{
if (x.Number > y.Number)
{
return 1;
}
if (x.Number < y.Number)
{
return -1;
}
return 0;
}
}
}
}
36 changes: 36 additions & 0 deletions linqsort/linqsort/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -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")]
Loading