@resk/core
Version:
An innovative TypeScript framework that empowers developers to build applications with a fully decorator-based architecture for efficient resource management. By combining the power of decorators with a resource-oriented design, DecorRes enhances code cla
55 lines (54 loc) • 2.3 kB
TypeScript
import { IResourceQueryOptionsOrderByDirection } from "../types/filters";
/**
* A highly optimized sorting function capable of efficiently handling billions of array elements
* with support for complex objects and various data types.
*
* @template T - The type of array elements being sorted
* @template V - The type of values being compared for sorting
*
* @param data - The array to be sorted
* @param getItemValue - Function that extracts the comparable value from each array item
* @param options - Configuration options to control the sorting behavior
*
* @returns The sorted array (either the original array modified in-place or a new array)
*
* @example
* // Sort an array of objects by their 'name' property
* const users = [
* { id: 101, name: "Alice", age: 28 },
* { id: 102, name: "bob", age: 34 },
* { id: 103, name: "Charlie", age: 21 }
* ];
*
* // Case-sensitive sort (default)
* const sortedByName = sortBy(users, user => user.name);
* // Result: [{ id: 101, name: "Alice", age: 28 }, { id: 103, name: "Charlie", age: 21 }, { id: 102, name: "bob", age: 34 }]
*
* // Case-insensitive sort
* const sortedIgnoringCase = sortBy(users, user => user.name, { ignoreCase: true });
* // Result: [{ id: 101, name: "Alice", age: 28 }, { id: 102, name: "bob", age: 34 }, { id: 103, name: "Charlie", age: 21 }]
*
* @example
* // Sort by date values in descending order (newest first)
* const tasks = [
* { id: 1, title: "Task 1", deadline: new Date("2023-12-01") },
* { id: 2, title: "Task 2", deadline: new Date("2023-05-15") },
* { id: 3, title: "Task 3", deadline: new Date("2024-02-20") }
* ];
*
* const sortedByDeadline = sortBy(tasks, task => task.deadline, { direction: 'desc' });
* // Result: Tasks ordered with newest deadline first
*
* @example
* // Create a new sorted array without modifying the original
* const numbers = [5, 2, 9, 1, 5, 6];
* const sortedNumbers = sortBy(numbers, n => n, { inPlace: false });
* // numbers is still [5, 2, 9, 1, 5, 6]
* // sortedNumbers is [1, 2, 5, 5, 6, 9]
*/
export declare function sortBy<T, V = any>(data: T[], getItemValue: (item: T) => V, options?: {
direction?: IResourceQueryOptionsOrderByDirection;
inPlace?: boolean;
chunkSize?: number;
ignoreCase?: boolean;
}): T[];