Skip to main content
Skip to main content

compare

BufferConstructor.compare

compare(buf1, buf2): 0 | 1 | -1

Compares buf1 to buf2, typically for the purpose of sorting arrays ofBuffer instances. This is equivalent to calling buf1.compare(buf2).

import { Buffer } from 'buffer';

const buf1 = Buffer.from('1234');
const buf2 = Buffer.from('0123');
const arr = [buf1, buf2];

console.log(arr.sort(Buffer.compare));
// Prints: [ <Buffer 30 31 32 33>, <Buffer 31 32 33 34> ]
// (This result is equal to: [buf2, buf1].)

Parameters

buf1Uint8ArrayRequired
buf2Uint8ArrayRequired

Returns

0 | 1 | -1

``0`` \| ``1`` \| ``-1``0 | 1 | -1
Either -1, 0, or 1, depending on the result of the comparison. See compare for details.

Since

v0.11.13

Was this section helpful?