Search code examples
c++sortingvisual-c++bufferpcm

Sorting a buffer of data packets with specific structure


I need to sort data packets which have been transmitted over a TCP server/network in ASCENDING order based on the SEQ number (which is just an unsigned 4 byte integer). In other words, I want to rearrange my data buffer (I am using std::vector for simplicity), so that the data is ordered in ASCENDING order by SEQ, followed by the respective CHECKSUM and LEN (length/size in bytes) of the packet, as well as the actual (byte) data of course...

Each data packet follows the structure outlined below, and there will be duplicate SEQ numbers (I shouldn't have too much difficulty verifying the good data packets using a CHECKSUM mechanism once I have the data in order).

Any ideas or assistance would be greatly appreciated, and I do apologize in advance, if this has been previously answered.


Solution

  • By your question. A vector<byte> represents your buffer

    Following are pseudo code for explanation.

    vector<byte> buffer;
    // receive data;
    
    // packet as a struct    
    vector<packet> packets = extract_packet( &buffer );
    
    sort(packets .begin(), packets .end(), 
    [](const packets & a, const packets & b) -> bool
    { 
        return a.SEQ < b.SEQ; 
    });