algorithm - Javascript: Find out of sequence dates -
consider nested array of dates , names:
var fdates = [ ['2015-02-03', 'name1'], ['2015-02-04', 'nameg'], ['2015-02-04', 'name5'], ['2015-02-05', 'nameh'], ['1929-03-12', 'name4'], ['2023-07-01', 'name7'], ['2015-02-07', 'name0'], ['2015-02-08', 'nameh'], ['2015-02-15', 'namex'], ['2015-02-09', 'namew'], ['1980-12-23', 'name2'], ['2015-02-12', 'namen'], ['2015-02-13', 'named'], ]
how can identify dates out of sequence. don't care if dates repeat, or skip, need ones out of order. ie, should back:
results = [ ['1929-03-12', 'name4'], ['2023-07-01', 'name7'], ['2015-02-15', 'namex'], ['1980-12-23', 'name2'], ]
('namex' less obvious, it's not in general order of list.)
this appears variation on longest increase subsequence (lis) problem, caveat there may repeated dates in sequence shouldn't ever step backward.
use case: have sorted , dated records , need find ones dates "suspicious" -- perhaps input error -- flag checking.
nb1: attempts far have been laughable, , not worth sharing.
nb2: using straight javascript , not framework. (i in node, looking package-free solution can understand what's going on...)
use javascript date type. compare objects. simplistically,
date1 = new date(fdates[i, 0]) date2 = new date(fdates[i+1, 0]) if (date2 < date1) { // or whatever comparison want ... // flag / print / alert date }
to clarify, merely finds items out of sequence. can strings, jaromanda x
pointed out. however, use phrase "way out of line"; whatever means you, date
should give ability determine , test it. instance, '2023-07-01' unacceptable because it's 8 years away, or because it's out of order 2015 dates? might want comparison simpler time span, such 1 month, comparison looks like
if (date2-date1 > one_month)
Comments
Post a Comment