当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Node.js Buffer.compare(buf1, buf2)用法及代码示例


静态方法:Buffer.compare(buf1, buf2)

历史
版本变化
v8.0.0

参数现在可以是 Uint8Array s。

v0.11.13

添加于:v0.11.13


参数

buf1buf2 进行比较,通常用于对 Buffer 实例的数组进行排序。这相当于调用 buf1.compare(buf2)

import { Buffer } from 'node: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].)const { Buffer } = require('node: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].)

相关用法


注:本文由纯净天空筛选整理自nodejs.org大神的英文原创作品 Buffer.compare(buf1, buf2)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。