Skip to content

Commit 1f86e54

Browse files
Sort exercise records by weight descending (#113)
- Updated record rendering to sort fetched exercise records before displaying them in the table. Records are now ordered by weight descending (heaviest first), and for equal weights by reps ascending (fewest reps first), matching your requested top-to-bottom order. - Switched empty-state and caption logic to use the sorted list, keeping the “no records” and context messaging behavior consistent after sorting. ------ [Codex Task](https://chatgpt.com/codex/tasks/task_e_698c3e1f243c83258e357104e9028d7f)
1 parent a8a51f4 commit 1f86e54

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

workout-exercise-record.html

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,18 +349,20 @@ <h2 style="margin: 0;">Workout tools</h2>
349349
noRecordNote.textContent = '';
350350
try {
351351
const data = await apiFetch(`/exercises/${encodeURIComponent(name)}/records`);
352-
(data.records || []).forEach((rec) => {
352+
const sortedRecords = sortRecords(data.records || []);
353+
354+
sortedRecords.forEach((rec) => {
353355
const row = document.createElement('tr');
354356
row.innerHTML = `<td>${rec.weight}</td><td>${rec.reps}</td>`;
355357
recordRows.appendChild(row);
356358
});
357-
if (!data.records || data.records.length === 0) {
359+
if (sortedRecords.length === 0) {
358360
const row = document.createElement('tr');
359361
row.innerHTML = '<td colspan="2">No personal bests yet.</td>';
360362
recordRows.appendChild(row);
361363
noRecordNote.textContent = `${displayName} has no records yet.`;
362364
}
363-
updateRecordsCaption(displayName, data.records && data.records.length > 0);
365+
updateRecordsCaption(displayName, sortedRecords.length > 0);
364366
loadStatus.textContent = '';
365367
} catch (error) {
366368
loadStatus.textContent = error.message;
@@ -376,6 +378,13 @@ <h2 style="margin: 0;">Workout tools</h2>
376378
);
377379
}
378380

381+
function sortRecords(records) {
382+
return [...records].sort((a, b) => {
383+
if (b.weight !== a.weight) return b.weight - a.weight;
384+
return a.reps - b.reps;
385+
});
386+
}
387+
379388
function clearRecordInputs() {
380389
recordWeight.value = '';
381390
recordReps.value = '';

0 commit comments

Comments
 (0)