| 1234567891011121314151617181920212223242526272829303132333435363738 |
- import type { AnyJson } from '~/types/data'
- export default class ArrayUtils {
- /**
- * 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)
- )
- })
- }
- }
|