Adding random app sort. Fixes #268 (#269)

This commit is contained in:
Dessalines 2023-11-03 09:50:12 -04:00 committed by GitHub
parent 15bd9d2844
commit 97c3e4aae6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 8 deletions

View File

@ -13,6 +13,7 @@ import {
} from "./app-definitions";
import { Icon } from "./icon";
import { I18nKeys } from "i18next";
import { sortRandom } from "../utils";
const TitleBlock = () => (
<div className="flex flex-col items-center pt-16 mb-4">
@ -146,6 +147,9 @@ export class Apps extends Component<any, State> {
apps = apps.filter(a => a.platforms.includes(this.state.platform));
}
// Random sort
apps = sortRandom(apps);
this.setState({ apps });
}

View File

@ -3,7 +3,7 @@ import { Helmet } from "inferno-helmet";
import { i18n, LANGUAGES } from "../i18next";
import { T } from "inferno-i18next";
import { instance_stats } from "../instance_stats";
import { getQueryParams, mdToHtml, numToSI } from "../utils";
import { getQueryParams, mdToHtml, numToSI, sortRandom } from "../utils";
import { Badge, SELECT_CLASSES, SectionTitle, TEXT_GRADIENT } from "./common";
import {
INSTANCE_HELPERS,
@ -351,13 +351,6 @@ const LEAST_ACTIVE_SORT: Sort = {
const SORTS: Sort[] = [RANDOM_SORT, MOST_ACTIVE_SORT, LEAST_ACTIVE_SORT];
function sortRandom(instances: any[]): any[] {
return instances
.map(value => ({ value, sort: Math.random() }))
.sort((a, b) => a.sort - b.sort)
.map(({ value }) => value);
}
function sortActive(instances: any[]): any[] {
return instances.sort(
(a, b) =>

View File

@ -57,3 +57,10 @@ export function monthsBetween(startDate: Date, endDate: Date) {
// Convert back to days and return
return Math.round(differenceMs / oneMonth);
}
export function sortRandom<T>(list: T[]): T[] {
return list
.map(value => ({ value, sort: Math.random() }))
.sort((a, b) => a.sort - b.sort)
.map(({ value }) => value);
}