Ver código fonte

添加 sortTime 公共方法

huangbin 4 anos atrás
pai
commit
887ed3e344
1 arquivos alterados com 17 adições e 1 exclusões
  1. 17 1
      src/utils/time/index.ts

+ 17 - 1
src/utils/time/index.ts

@@ -3,7 +3,7 @@ import moment, { DurationInputArg1, Moment, unitOfTime } from "moment";
 
 /**
  * 获取number类型时间戳
- * @returns 
+ * @returns
  */
 export function getTimeValue(): number {
     return moment().valueOf()
@@ -54,4 +54,20 @@ export function debounce(fn: () => void, wait: number, timer: keyof TimeoutTimer
         timerUtil.clearTimeout(timer);
         timerUtil.setTimeout(fn, wait, timer);
     })();
+}
+
+/**
+ * 时间排序
+ * @param arr
+ * @param key
+ * @param isUp true: 升序,false: 倒序
+ * @returns
+ */
+export function sortTime<T extends object>(arr: T[], key: keyof T, isUp = true) {
+    const result = arr.sort((a, b) => {
+        const time1 = new Date((a[key] as unknown) as string).getTime();
+        const time2 = new Date((b[key] as unknown) as string).getTime();
+        return isUp ? time1 - time2 : time2 - time1;
+    });
+    return result;
 }