import type { AnyJson } from '~/types/data' export default class ArrayUtils { // Private constructor to prevent instantiation private constructor() { // This utility class is not meant to be instantiated } /** * Trie un tableau * * @param array * @param reverse */ public static sort( array: Array, reverse: boolean = false, ): Array { return array.sort((a, b) => { return (a < b ? -1 : a > b ? 1 : 0) * (reverse ? -1 : 1) }) } /** * Trie un tableau d'objets selon une propriété commune * * @param array Une array d'objets * @param property Le nom d'une propriété possédée par tous les objets * @param reverse */ public static sortObjectsByProp( array: Array, property: string, reverse: boolean = false, ): Array { return array.sort((a: AnyJson, b: AnyJson) => { return ( (a[property] < b[property] ? -1 : a[property] > b[property] ? 1 : 0) * (reverse ? -1 : 1) ) }) } }