Complete API reference and usage guide for The Best Sort library
npm install the-best-sortimport { SortableNumber, SorterBuilder, ConcreteSortingStrategyFactory, StrategyType } from 'the-best-sort';
const numbers = [1, 100, 10];
const sortableArray = numbers.map(n => new SortableNumber(n));
const factory = new ConcreteSortingStrategyFactory<SortableNumber>();
const strategy = factory.createStrategy(StrategyType.DEFAULT);
const sorter = new SorterBuilder<SortableNumber>()
.setArray(sortableArray)
.setStrategy(strategy)
.build();
const sortedArray = await sorter.execute();
console.log(sortedArray);A comparable number wrapper implementing the ISortable interface
const nums = [1, 2, 3].map( n => new SortableNumber(n) );
Manages state and event notifications during sorting
const context = new SortingContext( "Strategy Name" ); context.attach(observer);
Fluent interface for constructing sorters with observers
new SorterBuilder() .setArray(array) .setStrategy(strategy) .addObserver(observer) .build();
Collects performance metrics during sorting execution
const stats = new StatisticsObserver(); sorter.addObserver(stats); stats.printStatistics();
Create and register sorting strategies
const factory = new ConcreteSortingStrategyFactory(); const strategy = factory.createStrategy(StrategyType.DEFAULT);
Singleton for global configuration management
ConfigurationManager.getInstance()
.updateConfig({
baseDelayMs: 100
});Encapsulate sorting operations as commands
import { CommandInvoker, ExecuteSortingCommand } from 'the-best-sort';
const invoker = new CommandInvoker();
const sortingCommand = new ExecuteSortingCommand(sorter);
invoker.enqueueCommand(sortingCommand);
const results = await invoker.executeAll();Use abstract runners for sorting workflows
import { LoggingSortingRunner } from 'the-best-sort';
const runner = new LoggingSortingRunner<SortableNumber>();
const sortedArray = await runner.run(sortableArray, StrategyType.DEFAULT);Control sorting behavior globally
import { ConfigurationManager } from 'the-best-sort';
ConfigurationManager.getInstance().updateConfig({
baseDelayMs: 100,
enableLogging: true,
showTimestamps: true,
colorize: true
});The library emits the following event types during sorting:
Sorting has started
An array element was processed and added to result
Sorting has completed
An error occurred during sorting
StatisticsObserver collects the following metrics during sorting:
interface SortingStatistics {
duration: number; // Total duration in milliseconds
sortedElements: number; // Number of elements sorted
totalDelay: number; // Sum of all delays
averageDelay: number; // Average delay per element
eventCounts: Map<EventType, number>; // Event counts by type
}The library follows a layered architecture:
The library leverages advanced TypeScript features:
Very large arrays (10,000+ elements) may cause performance degradation due to the asynchronous nature of the sleep sort algorithm and event emission overhead.