Improve the order of list vercel blob

Hello

Currently the order of list blobs is random.

import { list } from "@vercel/blob";

// random order of blobs
const res = list() 

It would be way easier and straight forward if the order is created_at descending, could that not be implemented either by default or via prop?

Thanks for considering this as an improvement

Thanks for reaching out. I don’t think this is possible right now so I have shared the feedback internally. Keep them coming! :slight_smile:

Thanks for the feedback @Dohomi! The ordering isn’t actually random - Vercel Blob returns blobs sorted lexicographically by pathname (like dictionary order), which can appear random when filenames don’t follow a predictable pattern.

Unfortunately, sorting by uploadedAt isn’t possible at the storage level, so if you want that you need to work around this limitation.

Workaround: Include timestamps in your blob pathnames for chronological sorting:


import { put } from "@vercel/blob";

// Include ISO timestamp for chronological order

const timestamp = new Date().toISOString(); // 2024-06-03T10:30:00.000Z

const blob = await put(`${timestamp}-filename.jpg`, file, { access: 'public' });

When you list() blobs with this naming pattern, they’ll automatically sort chronologically (newest first if you want reverse order, manipulate the timestamp).

Alternatively, you can sort client-side after retrieval:


const res = await list();

const sortedBlobs = res.blobs.sort((a, b) => new Date(b.uploadedAt) - new Date(a.uploadedAt));

Thanks!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.