Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions app/ycode/api/collections/[id]/items/filter/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,16 +537,16 @@ export async function POST(
// then hydrate only the requested page window.
const sortValueByItem = await getFieldValuesForItems(sortBy, true, matchingIds);
const sortedIds = [...matchingIds].sort((a, b) => {
const aVal = sortValueByItem.get(a) || '';
const bVal = sortValueByItem.get(b) || '';
const aNum = parseFloat(String(aVal));
const bNum = parseFloat(String(bVal));
const aStr = String(sortValueByItem.get(a) || '');
const bStr = String(sortValueByItem.get(b) || '');
const aNum = aStr.trim() !== '' ? Number(aStr) : NaN;
const bNum = bStr.trim() !== '' ? Number(bStr) : NaN;
if (!isNaN(aNum) && !isNaN(bNum)) {
return sortOrder === 'desc' ? bNum - aNum : aNum - bNum;
}
return sortOrder === 'desc'
? String(bVal).localeCompare(String(aVal))
: String(aVal).localeCompare(String(bVal));
? bStr.localeCompare(aStr)
: aStr.localeCompare(bStr);
});
pageItemIds = sortedIds.slice(pageOffset, pageOffset + pageLimit);
if (pageItemIds.length > 0) {
Expand Down
12 changes: 5 additions & 7 deletions app/ycode/api/collections/[id]/items/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,19 +126,17 @@ export async function GET(
} else {
// Sort by field value (globally, before pagination)
items = [...items].sort((a, b) => {
const aValue = a.values[sortBy] || '';
const bValue = b.values[sortBy] || '';
const aStr = String(a.values[sortBy] || '');
const bStr = String(b.values[sortBy] || '');

// Try numeric comparison
const aNum = parseFloat(String(aValue));
const bNum = parseFloat(String(bValue));
const aNum = aStr.trim() !== '' ? Number(aStr) : NaN;
const bNum = bStr.trim() !== '' ? Number(bStr) : NaN;

if (!isNaN(aNum) && !isNaN(bNum)) {
return sortOrder === 'asc' ? aNum - bNum : bNum - aNum;
}

// String comparison
const comparison = String(aValue).localeCompare(String(bValue));
const comparison = aStr.localeCompare(bStr);
return sortOrder === 'asc' ? comparison : -comparison;
});
}
Expand Down
8 changes: 5 additions & 3 deletions lib/page-fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1781,14 +1781,16 @@ export async function resolveCollectionLayers(
sortedItems = items.sort((a, b) => {
const aValue = a.values[sortBy] || '';
const bValue = b.values[sortBy] || '';
const aNum = parseFloat(String(aValue));
const bNum = parseFloat(String(bValue));
const aStr = String(aValue);
const bStr = String(bValue);
const aNum = aStr.trim() !== '' ? Number(aStr) : NaN;
const bNum = bStr.trim() !== '' ? Number(bStr) : NaN;

if (!isNaN(aNum) && !isNaN(bNum)) {
return sortOrder === 'desc' ? bNum - aNum : aNum - bNum;
}

const comparison = String(aValue).localeCompare(String(bValue));
const comparison = aStr.localeCompare(bStr);
return sortOrder === 'desc' ? -comparison : comparison;
});
}
Expand Down