0

I have a csv file which contains the following values:

18/10/2013,  news item 1
18/10/2013,  news item 2
17/10/2013,  news item 3
16/10/2013,  news item 4

How do I go about putting this into an array in JavaScript, ordered by date?

Once I have got it into an array, I also need to get the text values.

So far I have something like this:

Function readTextFile(){

var rawFile = new XMLhttpRequest();
Var myArray;

rawFile.open("GET", csvfile, true);
rawFile.onreadystatechange = function(){

if(rawFile.readyState === 4){
  if(rawFile.Status === 200 || rawFile.Status === 0)
{

}
}
}

Sorry if the text above is not formatted properly, I am posting from my phone. thanks

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
pmillio
  • 1,089
  • 2
  • 13
  • 20
  • possible duplicate of [Javascript code to parse CSV data](http://stackoverflow.com/questions/1293147/javascript-code-to-parse-csv-data) – Joren Oct 18 '13 at 10:17

2 Answers2

2

This is how you can do it.

Function readTextFile(){

var rawFile = new XMLhttpRequest();
Var myArray;

rawFile.open("GET", csvfile, true);
rawFile.onreadystatechange = function(){

if(rawFile.readyState === 4){
  if(rawFile.Status === 200 || rawFile.Status === 0)
{
    var response = rawFile.responseText;
    var splitData = new Array();
     //split data with new line

    splitData = response.split("\n");    //stores all the values separated by new line
    console.log(splitData[0]);      //returns   18/10/2013,  news item 1

  //split single line data with comma 

    var splitComma = new Array();
    var splitComma = splitData[0].split(",");  
    console.log(splitComma[0]);          //returns  18/10/2013

   //start comparing date values here

}
}
}
raduns
  • 765
  • 7
  • 18
0
var myArray = rawFile.responseText.split(",");

But it will not sort the data according to login date

Anand
  • 14,545
  • 8
  • 32
  • 44