Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added new Natural String Sorting Kata

  • Loading branch information...
commit b4a1086e154cb808206b495c8553c809cda8fcaa 1 parent b78391f
@garora authored
View
3  TDD-Katas-project/TDD-Katas-project/TDD-Katas-project.csproj
@@ -62,12 +62,15 @@
<Compile Include="The CalcStats Kata\CalcStatTest.cs" />
<Compile Include="The LeapYear Kata\LeapYear.cs" />
<Compile Include="The LeapYear Kata\LeapYearTest.cs" />
+ <Compile Include="The Natural String Sorting Kata\NaturalStringSorting.cs" />
+ <Compile Include="The Natural String Sorting Kata\NaturalStringSortingTest.cs" />
<Compile Include="The PrimeFactor Kata\PrimeFactor.cs" />
<Compile Include="The PrimeFactor Kata\PrimeFactorTest.cs" />
<Compile Include="The RecentlyUsedList kata\RecentlyUsedList.cs" />
<Compile Include="The RecentlyUsedList kata\RecentlyUsedListTest.cs" />
<Compile Include="The WordWrap Kata\WordWrap.cs" />
<Compile Include="The WordWrap Kata\WordWrapTest.cs" />
+ <Compile Include="Utility\EnumerableComparer.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="BowlingGame\TheBowlingGameKata.txt" />
View
67 TDD-Katas-project/TDD-Katas-project/The Natural String Sorting Kata/NaturalStringSorting.cs
@@ -0,0 +1,67 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text.RegularExpressions;
+using TDD_Katas_project.Utility;
+
+namespace TDD_Katas_project.The_Natural_String_Sorting_Kata
+{
+ public class NaturalStringSorting
+ {
+ #region Member variables
+ public enum SortOrder
+ {
+ Ascending, Descending
+ }
+ #endregion
+
+ #region Public Methods
+
+ public List<string> SortString(string[] strItems)
+ {
+ return SortString(strItems, SortOrder.Ascending);
+ }
+
+ public List<string> SortString(string[] strItems, SortOrder order)
+ {
+ Func<string, object> convert = str =>
+ {
+ try
+ {
+ return int.Parse(str);
+ }
+ catch
+ {
+ return str;
+ }
+ };
+
+ return GetSortedList(strItems, order, convert);
+
+ }
+ #endregion
+
+ #region Private Method
+
+ private static List<string> GetSortedList(IEnumerable<string> strItems, SortOrder order, Func<string, object> convert)
+ {
+ List<string> sorted;
+ switch (order)
+ {
+ case SortOrder.Descending:
+ sorted = strItems.OrderByDescending(
+ str => Regex.Split(str.Replace(" ", ""), "([0-9]+)").Select(convert),
+ new EnumerableComparer<object>()).ToList();
+ break;
+ default:
+ sorted = strItems.OrderBy(
+ str => Regex.Split(str.Replace(" ", ""), "([0-9]+)").Select(convert),
+ new EnumerableComparer<object>()).ToList();
+ break;
+ }
+ return sorted;
+ }
+
+ #endregion
+ }
+}
View
58 ...tas-project/TDD-Katas-project/The Natural String Sorting Kata/NaturalStringSortingTest.cs
@@ -0,0 +1,58 @@
+using System.Collections.Generic;
+using System.Linq;
+using NUnit.Framework;
+
+namespace TDD_Katas_project.The_Natural_String_Sorting_Kata
+{
+ [TestFixture]
+ [Category("The Natural String Kata")]
+ public class NaturalStringSortingTest
+ {
+ #region Private Member Variables
+ private NaturalStringSorting _naturalStringSorting;
+ private string[] _strings;
+ #endregion
+
+ #region Setup/TearDown
+ [TestFixtureSetUp]
+ public void Setup()
+ {
+ _naturalStringSorting = new NaturalStringSorting();
+ _strings = new[] { "a1", "1", "3", "2", "b1", "1a", "b3", "23", "z 21", "21 1", "z22", "0" };
+ }
+ [TestFixtureTearDown]
+ public void TearDown()
+ {
+ _naturalStringSorting = null;
+ _strings = null;
+ }
+ #endregion
+
+ #region Test Methods
+
+ [Test]
+ public void CanSortStringDefaultOrder()
+ {
+ var sortString = _naturalStringSorting.SortString(_strings);
+ string[] result = { "0", "1", "1a", "2", "3", "23", "21 1", "a1", "b1", "b3", "z 21", "z22" };
+ Assert.That(sortString, Is.EqualTo(ToList(result)));
+ }
+ [Test]
+ public void CanSortStringDesOrder()
+ {
+ var sortString = _naturalStringSorting.SortString(_strings, NaturalStringSorting.SortOrder.Descending);
+ string[] result = { "z22", "z 21", "b3", "b1", "a1", "21 1", "23", "3", "2", "1a", "1", "0" };
+ Assert.That(sortString, Is.EqualTo(ToList(result)));
+
+ }
+ #endregion
+
+ #region Private Methods
+ private List<string> ToList(params string[] items)
+ {
+ return items.ToList();
+ }
+
+ #endregion
+ }
+}
View
69 TDD-Katas-project/TDD-Katas-project/Utility/EnumerableComparer.cs
@@ -0,0 +1,69 @@
+//Adapted from : http://www.interact-sw.co.uk/iangblog/2007/12/13/natural-sorting
+//Used whole code as it, still looking if there is any scope of improvement
+using System.Collections.Generic;
+
+namespace TDD_Katas_project.Utility
+{
+ /// <summary>
+ /// Compares two sequences.
+ /// </summary>
+ /// <typeparam name="T">Type of item in the sequences.</typeparam>
+ /// <remarks>
+ /// Compares elements from the two input sequences in turn. If we
+ /// run out of list before finding unequal elements, then the shorter
+ /// list is deemed to be the lesser list.
+ /// </remarks>
+ public class EnumerableComparer<T> : IComparer<IEnumerable<T>>
+ {
+ /// <summary>
+ /// Create a sequence comparer using the default comparer for T.
+ /// </summary>
+ public EnumerableComparer()
+ {
+ _comp = Comparer<T>.Default;
+ }
+
+ /// <summary>
+ /// Create a sequence comparer, using the specified item comparer
+ /// for T.
+ /// </summary>
+ /// <param name="comparer">Comparer for comparing each pair of
+ /// items from the sequences.</param>
+ public EnumerableComparer(IComparer<T> comparer)
+ {
+ _comp = comparer;
+ }
+
+ /// <summary>
+ /// Object used for comparing each element.
+ /// </summary>
+ private readonly IComparer<T> _comp;
+
+
+ /// <summary>
+ /// Compare two sequences of T.
+ /// </summary>
+ /// <param name="x">First sequence.</param>
+ /// <param name="y">Second sequence.</param>
+ public int Compare(IEnumerable<T> x, IEnumerable<T> y)
+ {
+ using (var leftIt = x.GetEnumerator())
+ using (var rightIt = y.GetEnumerator())
+ {
+ while (true)
+ {
+ var left = leftIt.MoveNext();
+ var right = rightIt.MoveNext();
+
+ if (!(left || right)) return 0;
+
+ if (!left) return -1;
+ if (!right) return 1;
+
+ var itemResult = _comp.Compare(leftIt.Current, rightIt.Current);
+ if (itemResult != 0) return itemResult;
+ }
+ }
+ }
+ }
+}

0 comments on commit b4a1086

Please sign in to comment.
Something went wrong with that request. Please try again.