CsvSorter 2.0.0

dotnet add package CsvSorter --version 2.0.0                
NuGet\Install-Package CsvSorter -Version 2.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CsvSorter" Version="2.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CsvSorter --version 2.0.0                
#r "nuget: CsvSorter, 2.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install CsvSorter as a Cake Addin
#addin nuget:?package=CsvSorter&version=2.0.0

// Install CsvSorter as a Cake Tool
#tool nuget:?package=CsvSorter&version=2.0.0                

CsvSorter

alternate text is missing from this package README image

Build Tests Issues License Nuget

It is a small package that will help you to sort your large CSV files.

Instalation

PM> Install-Package CsvSorter
> dotnet add package CsvSorter
<PackageReference Include="CsvSorter" Version="2.0.0" />

Dependencies

Package name Version
CsvHelper >=30.0.1

Avaliable methods

Method name Parameter type Description
Using CsvConfiguration Sets CsvConfiguration. See CsvHelper documentation
TypeConverterOptions Sets TypeConverterOptions. See CsvHelper documentation
IIndexProvider<T> or IAsyncIndexProvider<T> Sets index provider. Default: MemoryIndexProvider
SortDirection Sets sorting direction.<br>Default: Ascending
ToFileAsync string, CancellationToken Saves sorted data to a file
ToFile string Saves sorted data to a file
ToWriterAsync TextWriter, CancellationToken Saves sorted data using provided writer
ToWriter TextWriter Saves sorted data using provided writer

Basic usage

using CsvSorter;

await new StreamReader(@"C:\my_large_file.csv")
    .GetCsvSorter<int>("id")
    .ToFileAsync(@"C:\my_large_file_sorted_by_id.csv");
	
// or

await new CsvSorter<int>(streamReader, "id")
    .ToFileAsync(@"C:\my_large_file_sorted_by_id.csv");

Index providers

Default index provider is MemoryIndexProvider<T>. It stores index data in the memory.<br> You can create your own provider (AzureIndexProvider for example) by implementing IIndexProvider<T> or IAsyncIndexProvider<T> interfaces:

public interface IIndexProvider<T> where T: IComparable<T>
{
    void Add(CsvSorterIndex<T> record);
    IEnumerable<CsvSorterIndex<T>> GetSorted(SortDirection sortDirection);
    void Clear();
}

public interface IAsyncIndexProvider<T> where T: IComparable<T>
{
    Task AddAsync(CsvSorterIndex<T> record, CancellationToken cancellationToken);
    IAsyncEnumerable<CsvSorterIndex<T>> GetSorted(SortDirection sortDirection, CancellationToken cancellationToken);
    Task ClearAsync(CancellationToken cancellationToken);
}

Please note that only one index provider can be used at a time.

await new CsvSorter<int>(streamReader, "id")
    .Using(new FirebaseIndexProvider<int>()) // will be ignored
    .Using(new AzureIndexProvider<int>())    // will be used
    .ToWriterAsync(writer);

Events

You can specify 4 events: OnIndexCreationStarted, OnIndexCreationFinished, OnSortingStarted and OnSortingFinished

await new StreamReader(@"C:\my_large_file.csv")
    .GetCsvSorter<int>(0)
    .OnIndexCreationStarted(() => { logger.Info("Index creation has started"); })
    .OnIndexCreationFinished(() => { logger.Info("Index creation completed"); })
    .OnSortingStarted(() => { logger.Info("Sorting has started"); })
    .OnSortingFinished(() => { logger.Info("Sorting completed"); })
    .ToWriterAsync(writer);

A few more examples

var csvConfig = new CsvConfiguration(CultureInfo.InvariantCulture)
{
    HasHeaderRecord = false
};

var dateTimeConverterOptions = new TypeConverterOptions
{ 
    Formats = new[] { "dd_MM_yyyy" }
};

await new StreamReader(@"C:\my_large_file.csv")
    .GetCsvSorter<DateTime>(3)
    .Using(SortDirection.Descending)
    .Using(csvConfig)
    .Using(dateTimeConverterOptions)	
    .ToFileAsync(@"C:\my_large_file_sorted_by_date.csv", cancellationToken);
var csvConfig = new CsvConfiguration(CultureInfo.InvariantCulture)
{
    Delimiter = "|"
};

await new StreamReader(@"C:\my_large_file.csv")
    .GetCsvSorter<string>("email")
    .Using(csvConfig)
    .Using(new AzureIndexProvider<string>())
    .ToWriterAsync(writer);
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.0 67 1/30/2025
1.1.0 212 5/25/2023
1.0.2 366 11/2/2021
1.0.1 337 6/1/2021
1.0.0 357 5/31/2021