buf.compare()

buf.compare(otherBuffer)

  • otherBuffer {Buffer}
  • 返回:{Number}
比较两个 Buffer 实例,无论 buf 在排序上靠前、靠后甚至与 otherBuffer 相同都会返回一个数字标识。比较是基于在每个 Buffer 的实际字节序列。
  • 如果 otherBuffer 和 buf 相同,则返回 0
  • 如果 otherBuffer 排在 buf 前面,则返回 1
  • 如果 otherBuffer 排在 buf 后面,则返回 -1
``` 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]

原文地址:https://www.cnblogs.com/lalalagq/p/9908612.html