Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

197
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

0

toLocalUpperCase() is a function not a property.

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

Note the extra brackets

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda