静态方法:Buffer.compare(buf1, buf2)
【Static method: Buffer.compare(buf1, buf2)】
buf1<Buffer> | <Uint8Array>buf2<Buffer> | <Uint8Array>- 返回值:<integer> 根据比较结果,可能为
-1、0或1。详情请参阅buf.compare()。
buf1 与 buf2 进行比较,通常用于对 Buffer 实例数组进行排序。这相当于调用 buf1.compare(buf2)。
【Compares buf1 to buf2, typically for the purpose of sorting arrays of
Buffer instances. This is equivalent to calling
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].)