Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

199
Vistas
how to sort data in the file from file System read method in JavaScript?

How to sort the data obtained from read filesystem method in javascript

fs.readFile('node1.txt',function(err,data){
   if(err){
      return console.log(err)    
   }

   data.toString().toLocaleUpperCase.sort(function (first, second){
      if(first<second){
         return -1
      } else if(first>second) {
         return 1
      } else {
         return 0
      }
   })
}

Error: TypeError: sort is not a function

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Basically what your code is trying to do here with the line data.toString().toLocaleUpperCase.sort is :

Taking the data, transforming it into a string with the toString() method and the sorting the string.

The problem here is that you're trying to sort a string and in Javascript, String has not sort method.

What you should do is transforming your string into an array with the split method (documentation here)

For example if you want to sort the file into words :

data.toString().split(' ').sort(function (first, second){
   if(first.toLocaleUpperCase() < second.toLocaleUpperCase()){
      return -1
   } else if(first.toLocaleUpperCase() > second.toLocaleUpperCase()){
      return 1
   } else{
      return 0
   }
})
about 4 years ago · Juan Pablo Isaza Denunciar

0

toLocalUpperCase() is a function not a property.

Try data.toString().toLocaleUpperCase().sort(...)

Note the extra brackets

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda