buf.compare()

本文详细介绍了Node.js中Buffer实例的compare方法,该方法用于比较两个Buffer实例的字节序列,返回值标识了排序位置。通过具体示例展示了如何使用此方法进行比较及排序操作。

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值