I'm new to node.js. The problem is reading from 2 (or more) streams which have sorted data, and producing a "sorted merge" of them. For example:
Stream A: 1 5 6 8
Stream B: 2 3 4 7
========================
Result: 1 2 3 4 5 6 7 8
In C++/Java/C# this has a pretty obvious solution, something like:
BufferedReader[] readers = new BufferedReader[2];
String[] lines = new String[2];
// fill lines with initial values from both readers
// ...
while (true) {
int earliestIndex = -1;
// ....
// determine earliestIndex by looping over lines and comparing them
if (earliestIndex < 0) break;
String line = lines[earliestIndex];
// do something with line
System.out.println(line);
// advance reader
lines[earliestIndex] = readers[earliestIndex].readLine();
}
But in node, this seems rather difficult. Any ideas?