Skip to content

Is interval.merge outputs the interval in ascending order ? #1690

Answered by dobon
nivekithan asked this question in Q&A
Discussion options

You must be logged in to vote

It is ascending order. Here's the relevant code, the sorting is on line 478.

luxon/src/interval.js

Lines 476 to 495 in 20139a3

static merge(intervals) {
const [found, final] = intervals
.sort((a, b) => a.s - b.s)
.reduce(
([sofar, current], item) => {
if (!current) {
return [sofar, item];
} else if (current.overlaps(item) || current.abutsStart(item)) {
return [sofar, current.union(item)];
} else {
return [sofar.concat([current]), item];
}
},
[[], null]
);
if (final) {
found.push(final);
}
return found;
}

And here is the relevant test. The test's input array with length 5 is con…

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@nivekithan
Comment options

Answer selected by nivekithan
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants