當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。