buf.compare(target[, targetStart[, targetEnd[, sourceStart[, sourceEnd]]]])
曆史
| 版本 | 變化 | 
|---|---|
| v8.0.0 | 
  | 
| v5.11.0 | 現在支持用於指定偏移的附加參數。  | 
| v0.11.13 | 添加於:v0.11.13  | 
參數
target<Buffer> | <Uint8Array> ABuffer或與之比較Uint8Arraybuf。targetStart<integer>target內開始比較的偏移量。 默認:0。targetEnd<integer>target中結束比較的偏移量(不包括在內)。 默認:target.length。sourceStart<integer>buf內開始比較的偏移量。 默認:0。sourceEnd<integer>buf中結束比較的偏移量(不包括在內)。 默認:。buf.length- 返回: <integer>
 
比較 buf 和 target 並返回一個數字,指示 buf 在排序順序中是在 target 之前、之後還是與 target 相同。比較基於每個 Buffer 中的實際字節序列。
- 如果 
target與buf相同,則返回0 1如果返回target應該來前buf排序時。-1如果返回target應該來後buf排序時。
import { Buffer } from 'node:buffer'; 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 console.log([buf1, buf2, buf3].sort(Buffer.compare)); // Prints: [ <Buffer 41 42 43>, <Buffer 41 42 43 44>, <Buffer 42 43 44> ] // (This result is equal to: [buf1, buf3, buf2].)const { Buffer } = require('node:buffer'); 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 console.log([buf1, buf2, buf3].sort(Buffer.compare)); // Prints: [ <Buffer 41 42 43>, <Buffer 41 42 43 44>, <Buffer 42 43 44> ] // (This result is equal to: [buf1, buf3, buf2].)
可選的 targetStart 、 targetEnd 、 sourceStart 和 sourceEnd 參數可用於分別將比較限製在 target 和 buf 內的特定範圍內。
import { Buffer } from 'node:buffer'; const buf1 = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9]); const buf2 = Buffer.from([5, 6, 7, 8, 9, 1, 2, 3, 4]); console.log(buf1.compare(buf2, 5, 9, 0, 4)); // Prints: 0 console.log(buf1.compare(buf2, 0, 6, 4)); // Prints: -1 console.log(buf1.compare(buf2, 5, 6, 5)); // Prints: 1const { Buffer } = require('node:buffer'); const buf1 = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9]); const buf2 = Buffer.from([5, 6, 7, 8, 9, 1, 2, 3, 4]); console.log(buf1.compare(buf2, 5, 9, 0, 4)); // Prints: 0 console.log(buf1.compare(buf2, 0, 6, 4)); // Prints: -1 console.log(buf1.compare(buf2, 5, 6, 5)); // Prints: 1
如果 targetStart < 0 、 sourceStart < 0 、 targetEnd > target.byteLength 或 sourceEnd > source.byteLength ,則拋出   。ERR_OUT_OF_RANGE 
相關用法
- Node.js Buffer buf.copy(target[, targetStart[, sourceStart[, sourceEnd]]])用法及代碼示例
 - Node.js Buffer buf.writeBigUInt64BE(value[, offset])用法及代碼示例
 - Node.js Buffer buf.toString([encoding[, start[, end]]])用法及代碼示例
 - Node.js Buffer buf.writeDoubleLE(value[, offset])用法及代碼示例
 - Node.js Buffer buf.writeBigInt64LE(value[, offset])用法及代碼示例
 - Node.js Buffer buf.keys()用法及代碼示例
 - Node.js Buffer buf.writeFloatLE(value[, offset])用法及代碼示例
 - Node.js Buffer buf.indexOf(value[, byteOffset][, encoding])用法及代碼示例
 - Node.js Buffer buf.readFloatLE([offset])用法及代碼示例
 - Node.js Buffer buf.readInt32LE([offset])用法及代碼示例
 - Node.js Buffer buf.writeInt8(value[, offset])用法及代碼示例
 - Node.js Buffer buf.swap32()用法及代碼示例
 - Node.js Buffer buf.writeInt32LE(value[, offset])用法及代碼示例
 - Node.js Buffer buf.writeIntLE(value, offset, byteLength)用法及代碼示例
 - Node.js Buffer buf.values()用法及代碼示例
 - Node.js Buffer buf.writeDoubleBE(value[, offset])用法及代碼示例
 - Node.js Buffer buf.length用法及代碼示例
 - Node.js Buffer buf.byteOffset用法及代碼示例
 - Node.js Buffer buf.readUInt32BE([offset])用法及代碼示例
 - Node.js Buffer buf.readDoubleLE([offset])用法及代碼示例
 - Node.js Buffer buf.writeFloatBE(value[, offset])用法及代碼示例
 - Node.js Buffer buf.readBigUInt64LE([offset])用法及代碼示例
 - Node.js Buffer buf.readUInt16BE([offset])用法及代碼示例
 - Node.js Buffer buf.writeInt16BE(value[, offset])用法及代碼示例
 - Node.js Buffer buf.swap64()用法及代碼示例
 
注:本文由純淨天空篩選整理自nodejs.org大神的英文原創作品 buf.compare(target[, targetStart[, targetEnd[, sourceStart[, sourceEnd]]]])。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
