Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Injectable } from "@angular/core";
- import { IIdea } from "app/core/model/idea.interface";
- import { IPost } from "app/core/model/post.interface";
- @Injectable()
- export class SortService {
- public sort(ideas: IIdea[], order: boolean): IIdea[] {
- // if ideas to sort are given
- if (ideas) {
- return ideas.sort((idea: IIdea, otherIdea: IIdea): number => {
- // sort ascending
- if (order) {
- // both ideas have due_dates set (getTime would error if not)
- if (idea.due_date && otherIdea.due_date) {
- // if both are dates are the same don't sort, if one is earlier/later than the other do sort.
- if (idea.due_date.getTime() === otherIdea.due_date.getTime()) {
- return 0;
- } else if (idea.due_date.getTime() <= otherIdea.due_date.getTime()) {
- return -1;
- } else {
- return 1;
- }
- // if the other date is null it's treated as earlier the other cases are analog
- } else if (idea.due_date && !otherIdea.due_date) {
- return 1;
- } else if (!idea.due_date && otherIdea.due_date) {
- return -1;
- } else {
- return 0;
- }
- // sort descending logic is analog to ascending
- } else {
- if (idea.due_date && otherIdea.due_date) {
- if (idea.due_date.getTime() === otherIdea.due_date.getTime()) {
- return 0;
- } else if (idea.due_date.getTime() >= otherIdea.due_date.getTime()) {
- return -1;
- } else {
- return 1;
- }
- } else if (idea.due_date && !otherIdea.due_date) {
- return -1;
- } else if (!idea.due_date && otherIdea.due_date) {
- return 1;
- } else {
- return 0;
- }
- }
- });
- }
- }
- public sortPost(posts: IPost[], order: boolean): IPost[] {
- if (posts) {
- return posts.sort((post: IPost, otherPost: IPost): number => {
- // sort ascending
- if (order) {
- // both ideas have due_dates set (getTime would error if not)
- if (post.publish_date && otherPost.publish_date) {
- // if both are dates are the same don't sort, if one is earlier/later than the other do sort.
- if (post.publish_date.getTime() === otherPost.publish_date.getTime()) {
- return 0;
- } else if (post.publish_date.getTime() <= otherPost.publish_date.getTime()) {
- return -1;
- } else {
- return 1;
- }
- // if the other date is null it's treated as earlier the other cases are analog
- } else if (post.publish_date && !otherPost.publish_date) {
- return 1;
- } else if (!post.publish_date && otherPost.publish_date) {
- return -1;
- } else {
- return 0;
- }
- // sort descending logic is analog to ascending
- } else {
- if (post.publish_date && otherPost.publish_date) {
- if (post.publish_date.getTime() === otherPost.publish_date.getTime()) {
- return 0;
- } else if (post.publish_date.getTime() >= otherPost.publish_date.getTime()) {
- return -1;
- } else {
- return 1;
- }
- } else if (post.publish_date && !otherPost.publish_date) {
- return -1;
- } else if (!post.publish_date && otherPost.publish_date) {
- return 1;
- } else {
- return 0;
- }
- }
- });
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement