Método Node.js Buffer.compare()

❮ Módulo de búfer


Ejemplo

Compruebe si dos objetos de búfer son iguales:

var buf1 = Buffer.from('abc');
var buf2 = Buffer.from('abc');
var x = Buffer.compare(buf1, buf2);
console.log(x);

var buf1 = Buffer.from('a');
var buf2 = Buffer.from('b');
var x = Buffer.compare(buf1, buf2);
console.log(x);

var buf1 = Buffer.from('b');
var buf2 = Buffer.from('a');
var x = Buffer.compare(buf1, buf2);
console.log(x);

Definición y uso

El método compare() compara dos objetos de búfer y devuelve un número que define sus diferencias:

0 si son iguales
1 si buf1 es mayor que buf2
-1 si buf1 es menor que buf2

Este método se puede utilizar para ordenar matrices que contienen búferes.


Sintaxis

 Buffer.compare(buf1, buf2);

Valores paramétricos

Parameter Description
buf1 Required. A Buffer
buf2 Required. A Buffer

Detalles técnicos

Valor de retorno: Un número (-1, 0 o 1)
Versión de Node.js: 0.11.13

Más ejemplos

Ejemplo

Use Buffer.compare() para ordenar una matriz de búferes:

var buf1 = Buffer.from('b');
var buf2 = Buffer.from('c');
var buf3 = Buffer.from('a');
var arr = [buf1, buf2, buf3];

//The array before sorting:
console.log(arr);

//Sort the array:
console.log(arr.sort(Buffer.compare));

❮ Módulo de búfer