FileHelpers - Sort Big File with Record Class

  • Contact / Feedback
  • Introduction
  • Download
  • Quick Start
  • Must Read
  • Examples
    • QuickStart
      • Read Delimited File
      • Write Delimited File
      • Read Fixed File
      • Write Fixed File
      • Read or Write Record by Record
      • Autoproperties
    • Missing Values
      • Handle Missing Values with Nullable
      • Handle Missing Values With FieldNullValue
    • Attributes
      • FieldTrim
      • FieldOrder
      • FixedLengthRecord FixedMode.AllowLessChars
    • Converters
      • Enum Converter
      • Custom Converter
    • Events And Notification
      • INotifyRead Interface
      • INotifyWrite Interface
      • Before/After Read Event Handling
      • Before/After Write Event Handling
    • ErrorHandling
      • ErrorMode.ThrowException
      • ErrorMode.IgnoreAndContinue
      • ErrorMode SaveAndContinue
    • Advanced
      • Dynamic Engine Options
      • Multiple Delimiters
      • Multi Record Processing
      • Smart Format Detector
    • Sorting
      • Sort Big File with Record Class
      • Sort Big File without Record Class 1
      • Sort Big File without Record Class 2
    • MasterDetail
      • Master Detail Custom Selector
      • Master Detail Common Selector
  • Credits
  • Invite us a beer
  • Contribute
  • Source Code
  • Api Docs (Sandcastle)
  1. Examples
  2. Sorting
  3. Sort Big File with Record Class
Previous: Smart Format Detector
Next: Sort Big File without Record Class 1
Click button to edit
Sorting: Sort Big File with Record Class
Shows how to sort a big file using a record class

If you need to sort a really big file (20Mb and more) you have the BigFileSorter

Implements External Sorting (wikipedia)

The Sorter will split the file in blocks, write them to temp files, and finally join all in a sorted file

SortingWithRecord.cs
// OrdersTab must be IComparable<OrdersTab>

// We recommend to split in blocks between 1 and 40 Mb
var sorter = new BigFileSorter<OrdersTab>(10*1024*1024); // 10 Mb blocks

sorter.Sort("unsorted.txt", "sorted.txt");
OrdersTab.cs
[DelimitedRecord("\t")]
public class OrdersTab
    : IComparable<OrdersTab>
{
    public int OrderID;

    public string CustomerID;

    public int EmployeeID;

    public DateTime OrderDate;

    [FieldConverter(ConverterKind.Date, "ddMMyyyy")]
    public DateTime RequiredDate;

    [FieldNullValue(typeof (DateTime), "2005-1-1")]
    public DateTime ShippedDate;

    public int ShipVia;

    public decimal Freight;

    #region IComparable<OrdersTab> Members

    public int CompareTo(OrdersTab other)
    {
        return this.OrderID.CompareTo(other.OrderID);
    }

    #endregion
}
Copyright © 2020 Devoo - Marcos Meli All rights reserved. Template Design by GeeksLabs