Skip to content

Instantly share code, notes, and snippets.

@tanraya
Last active May 3, 2016 22:42
Show Gist options
  • Save tanraya/86b125a7bf1e593cf39374cda10f3b1b to your computer and use it in GitHub Desktop.
Save tanraya/86b125a7bf1e593cf39374cda10f3b1b to your computer and use it in GitHub Desktop.
var data = [
[0, 5],
[0, 5],
[2, 5],
[5, 6],
[6, 8],
[6, 11],
];
// Сортировка по левому значению
// Меньшие левые числа идут первее.
data.sort(function (a, b) {
if (a[0] > b[0]) return 1;
if (a[0] < b[0]) return -1;
return 0;
});
// Сортировка по правому значению
// Большие правые числа идут первее.
data.sort(function (a, b) {
if (a[0] == b[0] && a[1] < b[1]) return 1;
return 0;
});
// Каждый следующий отрезок должен быть спереди предыдущего и самым длинным
var line = [0, 0];
var result = [];
data.forEach(function(x) {
if (x[0] >= line[0] && x[1] > line[1]) {
result.push(line = [x[0], x[1]]);
}
});
console.log(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment