๐ง Episode Sorting Fix
โ Issue Identified
Problem: Episode column sorting was not working correctly - it was sorting by episode ID instead of episode name.
Root Cause: The column was using accessorKey: 'episode_id' which sorts numerically by ID, but displays episode names in the cell.
๐ Technical Analysis
The issue was a mismatch between what's displayed and what's sorted:
โ Before (Broken)
accessorKey: 'episode_id'
// Sorts by: 1, 2, 3, 4...
// Displays: "Episode A", "Episode B", "Episode C"...
Problem:
- Sorting by numeric ID (1, 2, 3)
- Displaying episode names ("Episode A", "Episode B")
- Sort order doesn't match visual order
โ
After (Fixed)
accessorFn: (row) => {
const episode = meta.episodes.find((e) => e.id === row.episode_id)
return episode ? episode.name : `Episode ${row.episode_id}`
}
// Sorts by: "Episode A", "Episode B", "Episode C"...
// Displays: "Episode A", "Episode B", "Episode C"...
Solution:
- Using
accessorFn to extract episode name
- Sorting alphabetically by episode name
- Sort order matches visual display
๐ Example Scenario
Consider shots from these episodes:
Episodes:
- ID: 1, Name: "Episode C - The Final Battle"
- ID: 2, Name: "Episode A - The Beginning"
- ID: 3, Name: "Episode B - The Journey"
Shots:
- Shot_001 (Episode ID: 1) โ "Episode C - The Final Battle"
- Shot_002 (Episode ID: 2) โ "Episode A - The Beginning"
- Shot_003 (Episode ID: 3) โ "Episode B - The Journey"
Before Fix (sorting by ID): Shot_001, Shot_002, Shot_003
After Fix (sorting by name): Shot_002, Shot_003, Shot_001
Now the visual order matches alphabetical episode names!
๐ง TanStack Table accessorFn
The fix uses TanStack Table's accessorFn instead of accessorKey:
// accessorKey - uses a simple property path
accessorKey: 'episode_id' // Gets row.episode_id
// accessorFn - uses a custom function to extract the sort value
accessorFn: (row) => {
const episode = meta.episodes.find((e) => e.id === row.episode_id)
return episode ? episode.name : `Episode ${row.episode_id}`
}
This allows us to sort by the computed episode name while still accessing the episode_id from the data.
โ
Fix Applied
Updated frontend/src/components/shot/columns.ts - Episode column:
- โ
Replaced
accessorKey: 'episode_id' with accessorFn
- โ
Now sorts by episode name instead of episode ID
- โ
Maintains same visual display
- โ
Preserves all existing functionality
๐งช Testing Instructions
To verify the episode sorting fix works correctly:
- Navigate to Shot Table: Go to any project's shots page with multiple episodes
- Check Episode Names: Ensure you have shots from episodes with different names (not just "Episode 1", "Episode 2")
- Test Episode Sort: Click on the "Episode" column header
- Verify Ascending: First click should sort episodes alphabetically AโZ by episode name
- Verify Descending: Second click should sort episodes reverse alphabetically ZโA by episode name
- Check Visual Order: The displayed episode names should be in alphabetical order, not numeric ID order
- Test with Mixed Names: Works best with episodes like "Episode A - Intro", "Episode C - Finale", "Episode B - Middle"
๐ฏ Expected Behavior
After the fix, clicking the Episode column header should:
1. No Sort (โ) โ Original data order
2. Ascending (โ) โ Episodes sorted A-Z by name: "Episode A", "Episode B", "Episode C"
3. Descending (โ) โ Episodes sorted Z-A by name: "Episode C", "Episode B", "Episode A"
4. Back to No Sort (โ) โ Original data order
Note: The sort is now based on episode names, not episode IDs, so the visual order will match the alphabetical order of episode names.
๐ Related Improvements
This fix also ensures:
- Consistency: Sort behavior matches what users see
- Usability: Users can sort episodes alphabetically by name
- Flexibility: Works with any episode naming convention
- Performance: Efficient lookup using episode metadata
โ
Fix Summary
Changed: accessorKey: 'episode_id' โ accessorFn: (row) => episode.name
Result: Episode column now sorts by episode name instead of episode ID
Impact: Episode sorting now works as expected and matches visual display
Status: Ready for testing