Files
KVideo/lib/utils/sort.ts
T
kuekhaoyang fad6041df0 feat: Implement source management modals and settings store
- Added AddSourceModal for adding new video sources with validation.
- Introduced ExportModal for exporting settings and history options.
- Created ImportModal for importing settings from JSON files.
- Developed SourceManager for managing video sources, including enabling/disabling, deleting, and reordering.
- Established settings store to manage application settings, including default sources and sorting options.
- Implemented sorting utility functions for video search results based on various criteria.
2025-11-19 11:53:37 +08:00

97 lines
2.6 KiB
TypeScript

/**
* Sort utility functions for search results
*/
import type { SortOption } from '@/lib/store/settings-store';
interface Video {
vod_id: string;
vod_name: string;
vod_pic?: string;
vod_remarks?: string;
vod_year?: string;
type_name?: string;
source: string;
sourceName?: string;
isNew?: boolean;
vod_play_url?: string;
vod_actor?: string;
vod_director?: string;
relevanceScore?: number;
latency?: number;
}
export function sortVideos(videos: Video[], sortBy: SortOption): Video[] {
const sorted = [...videos];
switch (sortBy) {
case 'relevance':
// Sort by relevance score (highest first)
return sorted.sort((a, b) => {
const scoreA = (a as any).relevanceScore || 0;
const scoreB = (b as any).relevanceScore || 0;
return scoreB - scoreA;
});
case 'latency-asc':
// Sort by latency (lowest first)
return sorted.sort((a, b) => {
const latencyA = a.latency || 99999;
const latencyB = b.latency || 99999;
return latencyA - latencyB;
});
case 'date-desc':
// Sort by year (newest first)
return sorted.sort((a, b) => {
const yearA = parseInt(a.vod_year || '0');
const yearB = parseInt(b.vod_year || '0');
return yearB - yearA;
});
case 'date-asc':
// Sort by year (oldest first)
return sorted.sort((a, b) => {
const yearA = parseInt(a.vod_year || '0');
const yearB = parseInt(b.vod_year || '0');
return yearA - yearB;
});
case 'rating-desc':
// Sort by rating if available (placeholder for future implementation)
return sorted.sort((a, b) => {
const ratingA = (a as any).vod_score || 0;
const ratingB = (b as any).vod_score || 0;
return ratingB - ratingA;
});
case 'name-asc':
// Sort by name A-Z
return sorted.sort((a, b) => {
return a.vod_name.localeCompare(b.vod_name, 'zh-CN');
});
case 'name-desc':
// Sort by name Z-A
return sorted.sort((a, b) => {
return b.vod_name.localeCompare(a.vod_name, 'zh-CN');
});
case 'default':
default:
// Default: by relevance then latency
return sorted.sort((a, b) => {
const scoreA = (a as any).relevanceScore || 0;
const scoreB = (b as any).relevanceScore || 0;
if (scoreA !== scoreB) {
return scoreB - scoreA;
}
const latencyA = a.latency || 99999;
const latencyB = b.latency || 99999;
return latencyA - latencyB;
});
}
}