2022-09-22 09:37:49 +00:00
|
|
|
import type { Stat, Topic, TopicStat } from '../graphql/types.gen'
|
2022-09-13 09:59:04 +00:00
|
|
|
|
2022-09-09 11:53:35 +00:00
|
|
|
export const byFirstChar = (a, b) => (a.name || a.title || '').localeCompare(b.name || b.title || '')
|
|
|
|
|
|
|
|
export const byCreated = (a: any, b: any) => {
|
|
|
|
const x = new Date(a?.createdAt)
|
|
|
|
const y = new Date(b?.createdAt)
|
|
|
|
|
|
|
|
if (x > y) return -1
|
|
|
|
|
|
|
|
if (x < y) return 1
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
export const byLength = (a: any[], b: any[]) => {
|
|
|
|
const x = a.length
|
|
|
|
const y = b.length
|
|
|
|
|
|
|
|
if (x > y) return -1
|
|
|
|
|
|
|
|
if (x < y) return 1
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2022-09-22 09:37:49 +00:00
|
|
|
// TODO more typing
|
2022-09-13 09:59:04 +00:00
|
|
|
export const byStat = (metric: keyof Stat) => {
|
2022-09-09 11:53:35 +00:00
|
|
|
return (a, b) => {
|
|
|
|
const x = (a?.stat && a.stat[metric]) || 0
|
|
|
|
const y = (b?.stat && b.stat[metric]) || 0
|
|
|
|
if (x > y) return -1
|
|
|
|
if (x < y) return 1
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-22 09:37:49 +00:00
|
|
|
export const byTopicStatDesc = (metric: keyof TopicStat) => {
|
|
|
|
return (a: Topic, b: Topic) => {
|
|
|
|
const x = (a?.stat && a.stat[metric]) || 0
|
|
|
|
const y = (b?.stat && b.stat[metric]) || 0
|
|
|
|
if (x > y) return -1
|
|
|
|
if (x < y) return 1
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-09 11:53:35 +00:00
|
|
|
export const sortBy = (data, metric) => {
|
|
|
|
const x = [...data]
|
|
|
|
x.sort(typeof metric === 'function' ? metric : byStat(metric))
|
|
|
|
return x
|
|
|
|
}
|