Advertisement
Guest User

Untitled

a guest
Feb 19th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Injectable } from "@angular/core";
  2. import { IIdea } from "app/core/model/idea.interface";
  3. import { IPost } from "app/core/model/post.interface";
  4.  
  5. @Injectable()
  6. export class SortService {
  7.  
  8.     public sort(ideas: IIdea[], order: boolean): IIdea[] {
  9.         // if ideas to sort are given
  10.         if (ideas) {
  11.             return ideas.sort((idea: IIdea, otherIdea: IIdea): number => {
  12.                 // sort ascending
  13.                 if (order) {
  14.                     // both ideas have due_dates set (getTime would error if not)
  15.                     if (idea.due_date && otherIdea.due_date) {
  16.                         // if both are dates are the same don't sort, if one is earlier/later than the other do sort.
  17.                         if (idea.due_date.getTime() === otherIdea.due_date.getTime()) {
  18.                             return 0;
  19.                         } else if (idea.due_date.getTime() <= otherIdea.due_date.getTime()) {
  20.                             return -1;
  21.                         } else {
  22.                             return 1;
  23.                         }
  24.                         // if the other date is null it's treated as earlier the other cases are analog
  25.                     } else if (idea.due_date && !otherIdea.due_date) {
  26.                         return 1;
  27.                     } else if (!idea.due_date && otherIdea.due_date) {
  28.                         return -1;
  29.                     } else {
  30.                         return 0;
  31.                     }
  32.                     // sort descending logic is analog to ascending
  33.                 } else {
  34.                     if (idea.due_date && otherIdea.due_date) {
  35.                         if (idea.due_date.getTime() === otherIdea.due_date.getTime()) {
  36.                             return 0;
  37.                         } else if (idea.due_date.getTime() >= otherIdea.due_date.getTime()) {
  38.                             return -1;
  39.                         } else {
  40.                             return 1;
  41.                         }
  42.                     } else if (idea.due_date && !otherIdea.due_date) {
  43.                         return -1;
  44.                     } else if (!idea.due_date && otherIdea.due_date) {
  45.                         return 1;
  46.                     } else {
  47.                         return 0;
  48.                     }
  49.                 }
  50.             });
  51.         }
  52.     }
  53.  
  54.     public sortPost(posts: IPost[], order: boolean): IPost[] {
  55.         if (posts) {
  56.             return posts.sort((post: IPost, otherPost: IPost): number => {
  57.                 // sort ascending
  58.                 if (order) {
  59.                     // both ideas have due_dates set (getTime would error if not)
  60.                     if (post.publish_date && otherPost.publish_date) {
  61.                         // if both are dates are the same don't sort, if one is earlier/later than the other do sort.
  62.                         if (post.publish_date.getTime() === otherPost.publish_date.getTime()) {
  63.                             return 0;
  64.                         } else if (post.publish_date.getTime() <= otherPost.publish_date.getTime()) {
  65.                             return -1;
  66.                         } else {
  67.                             return 1;
  68.                         }
  69.                         // if the other date is null it's treated as earlier the other cases are analog
  70.                     } else if (post.publish_date && !otherPost.publish_date) {
  71.                         return 1;
  72.                     } else if (!post.publish_date && otherPost.publish_date) {
  73.                         return -1;
  74.                     } else {
  75.                         return 0;
  76.                     }
  77.                     // sort descending logic is analog to ascending
  78.                 } else {
  79.                     if (post.publish_date && otherPost.publish_date) {
  80.                         if (post.publish_date.getTime() === otherPost.publish_date.getTime()) {
  81.                             return 0;
  82.                         } else if (post.publish_date.getTime() >= otherPost.publish_date.getTime()) {
  83.                             return -1;
  84.                         } else {
  85.                             return 1;
  86.                         }
  87.                     } else if (post.publish_date && !otherPost.publish_date) {
  88.                         return -1;
  89.                     } else if (!post.publish_date && otherPost.publish_date) {
  90.                         return 1;
  91.                     } else {
  92.                         return 0;
  93.                     }
  94.                 }
  95.             });
  96.         }
  97.     }
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement