SmartSortingAPI 1.0.0

Sorting algorithms library built with .NET standard to use over arrays and enumerations.

Install-Package SmartSortingAPI -Version 1.0.0
dotnet add package SmartSortingAPI --version 1.0.0
<PackageReference Include="SmartSortingAPI" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SmartSortingAPI --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Smart Sorting API

Build Status

Sort algorithms library to use with .NET Standard. All algorithms work over arrays and enumerations of items that implement IComparable<T> interface.

The following algorithms are implemented:

  • Bubble Sort
  • Heap Sort
  • Insertion Sort
  • Merge Sort
  • Quick Sort
  • Selection Sort

How to use

To use extension methods to sort data, add the following code block at the top of your .CS file:

using SmartSorting.Extensions.SortableExtensions;

If you prefer, you can create instances by hand of the sort algorithms from SmartSorting.Algorithms namespace.

Sorting arrays

To sort an array, use extension method SortUsing as below:

int[] array = { 1, 3, 4, 5, 2 };
array.SortUsing(ESortAlgorithm.BubbleSort, ESortOrder.Ascending);

The first parameter defines the algorithm to sort the data structure, and the second one indicates wheter we want to sort data in ascending or descending direction.

Sorting Enumerations

You can sort every data structure that implements IEnumerable&lt;T&gt; calling SortUsing:

var integerEnumeration = new List<int> { 1, 3, 5, 4, 2 };
var orderedEnumeration = integerEnumeration.SortUsing(ESortAlgorithm.MergeSort, ESortOrder.Descending);

Sorting Enumerations of Custom Types

You can also sort arrays and enumerations of your own types and also type from .NET library that implement IComparable&lt;T&gt;. Let's take a look at the following example:

public class TodoItem : IComparable<TodoItem>
{
  public int Id { get; private set; }

  public TodoItem(int id)
  {
    this.Id = id;
  }
  
  public int CompareTo(TodoItem other)
  {
    return this.Id.CompareTo(other.Id);
  }
}

The TodoItem class implements a comparison between other items of the same type by comparing the IDs. With this implementation we have a way to check which items are "smaller than", "bigger than" or equals to others. This way we can sort arrays or enumerations of this type:

var enumeration = new List<TodoItem>
{
  new TodoItem(1),
  new TodoItem(5),
  new TodoItem(3),
  new TodoItem(2),
  new TodoItem(4),
};

var orderedEnumeration = enumeration.SortUsing(ESortAlgorithm.SelectionSort, ESortOrder.Ascending);

Fast Sorting with Multiple Algorithms

Sometimes we are not sure about which algorithm will sort our enumeration faster. The library provides an extensions method which we can call to run many sort algorithms asynchronously over an enumeration, this way getting the sorted data structure from the first algorithm that finished its execution.

var enumeration = new List<TodoItem>
{
  new TodoItem(1),
  new TodoItem(5),
  new TodoItem(3),
  new TodoItem(2),
  new TodoItem(4),
};

var resultingEnumeration = await enumeration.FastSortUsingAsync(ESortOrder.Ascending, ESortAlgorithm.BubbleSort, ESortAlgorithm.InsertionSort);

Smart Sorting API

Build Status

Sort algorithms library to use with .NET Standard. All algorithms work over arrays and enumerations of items that implement IComparable<T> interface.

The following algorithms are implemented:

  • Bubble Sort
  • Heap Sort
  • Insertion Sort
  • Merge Sort
  • Quick Sort
  • Selection Sort

How to use

To use extension methods to sort data, add the following code block at the top of your .CS file:

using SmartSorting.Extensions.SortableExtensions;

If you prefer, you can create instances by hand of the sort algorithms from SmartSorting.Algorithms namespace.

Sorting arrays

To sort an array, use extension method SortUsing as below:

int[] array = { 1, 3, 4, 5, 2 };
array.SortUsing(ESortAlgorithm.BubbleSort, ESortOrder.Ascending);

The first parameter defines the algorithm to sort the data structure, and the second one indicates wheter we want to sort data in ascending or descending direction.

Sorting Enumerations

You can sort every data structure that implements IEnumerable&lt;T&gt; calling SortUsing:

var integerEnumeration = new List<int> { 1, 3, 5, 4, 2 };
var orderedEnumeration = integerEnumeration.SortUsing(ESortAlgorithm.MergeSort, ESortOrder.Descending);

Sorting Enumerations of Custom Types

You can also sort arrays and enumerations of your own types and also type from .NET library that implement IComparable&lt;T&gt;. Let's take a look at the following example:

public class TodoItem : IComparable<TodoItem>
{
  public int Id { get; private set; }

  public TodoItem(int id)
  {
    this.Id = id;
  }
  
  public int CompareTo(TodoItem other)
  {
    return this.Id.CompareTo(other.Id);
  }
}

The TodoItem class implements a comparison between other items of the same type by comparing the IDs. With this implementation we have a way to check which items are "smaller than", "bigger than" or equals to others. This way we can sort arrays or enumerations of this type:

var enumeration = new List<TodoItem>
{
  new TodoItem(1),
  new TodoItem(5),
  new TodoItem(3),
  new TodoItem(2),
  new TodoItem(4),
};

var orderedEnumeration = enumeration.SortUsing(ESortAlgorithm.SelectionSort, ESortOrder.Ascending);

Fast Sorting with Multiple Algorithms

Sometimes we are not sure about which algorithm will sort our enumeration faster. The library provides an extensions method which we can call to run many sort algorithms asynchronously over an enumeration, this way getting the sorted data structure from the first algorithm that finished its execution.

var enumeration = new List<TodoItem>
{
  new TodoItem(1),
  new TodoItem(5),
  new TodoItem(3),
  new TodoItem(2),
  new TodoItem(4),
};

var resultingEnumeration = await enumeration.FastSortUsingAsync(ESortOrder.Ascending, ESortAlgorithm.BubbleSort, ESortAlgorithm.InsertionSort);

Dependencies

This package has no dependencies.

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.0.0 150 9/25/2018