buf.compare(otherBuffer)
Compares two Buffer instances and returns a number indicating whether buf
comes before, after, or is the same as the otherBuffer
in sort order. Comparison is based on the actual sequence of bytes in each Buffer.
-
0
is returned ifotherBuffer
is the same asbuf
-
1
is returned ifotherBuffer
should come beforebuf
when sorted. -
-1
is returned ifotherBuffer
should come afterbuf
when sorted.
const buf1 = Buffer.from('ABC'); const buf2 = Buffer.from('BCD'); const buf3 = Buffer.from('ABCD'); console.log(buf1.compare(buf1)); // Prints: 0 console.log(buf1.compare(buf2)); // Prints: -1 console.log(buf1.compare(buf3)); // Prints: 1 console.log(buf2.compare(buf1)); // Prints: 1 console.log(buf2.compare(buf3)); // Prints: 1 [buf1, buf2, buf3].sort(Buffer.compare); // produces sort order [buf1, buf3, buf2]
Please login to continue.