0
[09.02.2017 - 10:40:06][NOTICE] - Start looping through invoices from Teamleader..
[08.02.2017 - 10:24:26][NOTICE] - Start looping through invoices from Teamleader..
[08.02.2017 - 10:29:24][NOTICE] - Start looping through invoices from Teamleader..

This is the code for producing the above output:

var data = allText.split("\n");
for(var i = 0, len = data.length; i < len; i++){
   console.log(data[i]);
}

Is it possible to sort the array on the given date and time?

here is an example of how it should look like:

[09.02.2017 - 10:40:06][NOTICE] - Start looping through invoices from Teamleader..
[08.02.2017 - 10:29:25][NOTICE] - Start looping through invoices from Teamleader..
[08.02.2017 - 10:24:26][NOTICE] - Start looping through invoices from Teamleader..
0

4 Answers 4

3

At the time of this writing the other solutions are either not complete or contain errors. In particular, the accepted answer by rakwaht sorts the dates as strings, which gives incorrect results.

You can use a regular expression and the Date constructor to extract and parse the date from a line. From there it's only a matter of sorting the array containing the lines:

const data = `
[09.02.2017 - 10:40:06][NOTICE] - Start looping through invoices from Teamleader..
[08.02.2017 - 10:24:26][NOTICE] - Start looping through invoices from Teamleader..
[08.02.2017 - 10:29:24][NOTICE] - Start looping through invoices from Teamleader..`
  .trim().split('\n')

function dateFromLine(line) {
  const result = /^\[(.*?)\]/.exec(line)
  const date = result[1].replace("-", "")
  return new Date(date)
}

function compareLinesByDate(a, b) {
  return dateFromLine(b) - dateFromLine(a)
}

data.sort(compareLinesByDate)
console.log(data.join('\n'))

Note that this code assumes that every line is formatted in this way. Some more work is necessary to make it more robust.

Sign up to request clarification or add additional context in comments.

5 Comments

I was a little too excited and accepted the other answer while you were typing this. If I check if typeof result === 'array' (dateFromLine(line)), the code doesn't fail when it passes a different string, right?
Only proceeding if result is an array should prevent run-time errors, but you still have to decide how to handle these malformed lines. Another thing to keep in mind is that for some lines result[1] could exist but not be a valid date, e.g. for the line [foo][NOTICE] - Start looping through invoices from Teamleader...
Definitely yours is a better approach 'cause is more fitting into the logic. Anyway when should my solution give wrong solutions? Obviously I assumed that the strings are all well formatted and all with the same pattern.
@rakwaht Strings in JavaScript are compared lexicographically, which means that the characters at the same position in each string are compared from start to end until it is established which string is greater than the other (or the two are equal). When comparing dates, however, we want to compare first by year, then by month, then by day and so on.
@rakwaht Consider [09.02.2017 - 10:40:06] and [08.02.2027 - 10:40:06] for example: Compared as dates, [09.02.2017 - 10:40:06] should come before [08.02.2027 - 10:40:06] (because 2017 is before 2027), but when compared as strings it is the other way around (because "08" comes before "09").
1

You can sort the array as you want with the sort function in which you can specify on which base it should be sorted:

Here an example that will fit your needs:

var data = [
"[09.02.2017 - 10:40:06][NOTICE] - Start looping through invoices from Teamleader]",
"[08.02.2017 - 10:24:26][NOTICE] - Start looping through invoices from Teamleader]",
"[08.02.2017 - 10:29:24][NOTICE] - Start looping through invoices from Teamleader]"
];
data.sort(
    function(a, b){ 
         // a and b are two elements in the list that are supposed to be compared
         var a_date = a.substring(1, 22); //take only date from string
         var b_date = b.substring(1, 22); //take only date from string
         // We compare those strings to order it.
         if ( a_date < b_date )
            return 1;
         if ( a_date > b_date  )
            return -1;
         return 0;
    }
);
 console.log(data);

An here you can find some references to the sort function in JS

Comments

0

You can use the function sort() on your array with a comparaison function to compare the date. I don't know what you are trying to compare but here is a simple example.

[/*...*/].sort(function(a, b){
    //here a and b stand for your dates. you will need to adjuste your code to  make it works since we have very little information about it.
    return Date.compare(a, b);
});

Comments

0

Well if i had to do this i would transform it in a real array then use a function like this :

function SortByDate(a, b){
  var aDateField = a.DateField.toLowerCase();
  var bDateField = b.DateField.toLowerCase(); 
  return ((aDateField < bNDateField) ? -1 : ((aDateField > bDateField) ? 1 : 0));
}

var data = allText.split("\n");
for(var i = 0, len = data.length; i < len; i++){
   //construct your array_data here 
}
var dataSortByDate = array_data.sort(SortByDate);

This is one of the comparaison function i used often. Hope it will help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.