A collection queue you can explain: stable multi-key sorting for unpaid invoices
If software tells a business owner "call this client first", it should also say why. A score from a model can't do that. A fixed list of sort keys can, and it gives the same answer every time. The keys, in orde
If software tells a business owner "call this client first", it should also say why. A score from a model can't do that. A fixed list of sort keys can, and it gives the same answer every time.
The keys, in order
- a missed payment promise first,
- then the larger undisputed balance,
- then more days overdue,
- then the earlier record, so ties never depend on input order.
Disputed amounts are taken out of the chase amount and listed in their own queue, because a dispute needs a conversation, not a reminder.
// A collection queue with a fixed, explainable order (stable multi-key sort)
const today = new Date('2026-11-30');
const inv = [
{ id: 'INV-104', client: 'Client A', balance: 420000, due: '2026-10-23', promiseMissed: true, disputed: 0 },
{ id: 'INV-117', client: 'Client B', balance: 185000, due: '2026-11-18', promiseMissed: false, disputed: 0 },
{ id: 'INV-121', client: 'Client C', balance: 600000, due: '2026-11-10', promiseMissed: false, disputed: 150000 },
{ id: 'INV-125', client: 'Client D', balance: 90000, due: '2026-12-15', promiseMissed: false, disputed: 0 },
];
const days = d => Math.max(0, Math.round((today - new Date(d)) / 864e5));
const rows = inv.map(i => ({ ...i, overdue: days(i.due), chase: i.balance - i.disputed }))
.filter(i => i.overdue > 0 && i.chase > 0);
const keys = [
[r => r.promiseMissed ? 0 : 1, 'missed promise'],
[r => -r.chase, 'larger undisputed balance'],
[r => -r.overdue, 'more days overdue'],
[r => r.id, 'earlier record'],
];
rows.sort((a, b) => { for (const [k] of keys) { const x = k(a), y = k(b); if (x < y) return -1; if (x > y) return 1; } return 0; });
for (const r of rows) console.log(r.id, r.client, '$' + (r.chase / 100).toFixed(2), r.overdue + 'd', r.promiseMissed ? 'missed promise' : '');
const disputes = inv.filter(i => i.disputed > 0).map(i => i.id + ' $' + (i.disputed / 100).toFixed(2));
console.log('dispute queue:', disputes.join(', '));
Output:
INV-104 Client A $4200.00 38d missed promise
INV-121 Client C $4500.00 20d
INV-117 Client B $1850.00 12d
dispute queue: INV-121 $1500.00
Client C owes $6,000, but only the undisputed $4,500 is chased; the $1,500 moves to the dispute queue. INV-125 is not due yet, so it is not in today's list at all.
Why not a weighted score?
A score like 0.4 Γ balance + 0.6 Γ days changes order when someone tweaks a weight, and nobody can explain a 0.73. Lexicographic keys are boring and auditable: the reason for each position is the first key where two invoices differ.
This is a simplified version of the ordering in Invoice Recovery Desk (paid, $4.90, with a free demo), which also shows which unpaid invoices would close a cash gap by a given date.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.