arrayUtils.ts 801 B

1234567891011121314151617181920212223242526272829
  1. export default class ArrayUtils {
  2. /**
  3. * Trie un tableau
  4. *
  5. * @param array
  6. * @param reverse
  7. */
  8. public static sort(array: Array<any>, reverse: boolean = false): Array<Object> {
  9. return array.sort((a, b) => {
  10. return ((a < b) ? -1 : (a > b ? 1 : 0)) * (reverse ? -1 : 1)
  11. })
  12. }
  13. /**
  14. * Trie un tableau d'objets selon une propriété commune
  15. *
  16. * @param array Une array d'objets
  17. * @param property Le nom d'une propriété possédée par tous les objets
  18. * @param reverse
  19. */
  20. public static sortObjectsByProp(array: Array<Object>, property: string, reverse: boolean = false): Array<Object> {
  21. return array.sort((a: any, b: any) => {
  22. return ((a[property] < b[property]) ? -1 : (a[property] > b[property] ? 1 : 0)) * (reverse ? -1 : 1)
  23. })
  24. }
  25. }