静态方法:Buffer.compare(buf1, buf2)
¥Static method: Buffer.compare(buf1, buf2)
-
buf1
<Buffer> | <Uint8Array> -
buf2
<Buffer> | <Uint8Array> -
返回:<integer>
-1
、0
或1
,取决于比较的结果。详见buf.compare()
。¥Returns: <integer> Either
-1
,0
, or1
, depending on the result of the comparison. Seebuf.compare()
for details.
比较 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].)