I have a file as follows:

The file consists of two parts: header and data.
Part of the data is divided into pages of the same size. Each page contains data for a specific metric. For storing data for one indicator, it may take several pages (not necessarily sequential). Each page consists of a page title and a page body. The page header has a βNext Pageβ field, which is the index of the next page containing data for the same metric. The body of the page contains real data. All pages have the same and fixed size (20 bytes for the header and 800 bytes for the body (if the amount of data is less than 800 bytes, 0 will be filled)).
Part of the header consists of 20,000 elements, each element has information about a specific metric (point 1 β point 20,000). The element has a field called "first page", which is actually the index of the first page containing the data for the metric.
File can be up to 10 GB.
Requirement: to reorder the file data as soon as possible, that is, pages containing data for one metric must be sequential and from metric 1 to metric 20,000 in accordance with the alphabetical order (part of the header must be updated accordingly).
The obvious approach: for each metric, read all the data for the metric (per page), write the data to a new file. But it takes a lot of time, especially when reading data from a file.
Are there any effective ways?
source share