| 123456789101112131415161718192021222324252627282930313233343536373839404142 |
- 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<object | string | number>,
- reverse: boolean = false,
- ): Array<object | string | number> {
- 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<AnyJson>,
- property: string,
- reverse: boolean = false,
- ): Array<AnyJson> {
- return array.sort((a: AnyJson, b: AnyJson) => {
- return (
- (a[property] < b[property] ? -1 : a[property] > b[property] ? 1 : 0) *
- (reverse ? -1 : 1)
- )
- })
- }
- }
|