/**
 * Sort utilities - Các hàm tiện ích để sắp xếp
 */

/**
 * Sort strings ascending using localeCompare
 * 
 * @param a - First string
 * @param b - Second string
 * @returns Comparison result for Array.sort()
 */
export function sortByNameAsc(a: string, b: string): number {
  return a.localeCompare(b);
}

/**
 * Sort strings descending using localeCompare
 * 
 * @param a - First string
 * @param b - Second string
 * @returns Comparison result for Array.sort()
 */
export function sortByNameDesc(a: string, b: string): number {
  return b.localeCompare(a);
}

/**
 * Sort array of objects by a string property ascending
 * 
 * @param array - Array to sort
 * @param keyFn - Function to extract sort key from object
 * @returns Sorted array (new array, not mutated)
 */
export function sortByProperty<T>(array: T[], keyFn: (item: T) => string): T[] {
  return [...array].sort((a, b) => keyFn(a).localeCompare(keyFn(b)));
}

/**
 * Sort Object.entries() by key ascending
 * 
 * @param entries - Array of [key, value] tuples
 * @returns Sorted array of entries
 */
export function sortEntriesByKey<V>(entries: [string, V][]): [string, V][] {
  return [...entries].sort((a, b) => a[0].localeCompare(b[0]));
}

/**
 * Get sorted keys from an object
 * 
 * @param obj - Object to get sorted keys from
 * @returns Array of keys sorted ascending
 */
export function getSortedKeys<T extends Record<string, unknown>>(obj: T): string[] {
  return Object.keys(obj).sort(sortByNameAsc);
}
