Skip to content

Commit

Permalink
sitemap generator: sort urls primary by number of dashes and secondar…
Browse files Browse the repository at this point in the history
…y alphabetically (thanks to this, urls of the main levels will be at the beginning)
  • Loading branch information
janreges committed Dec 14, 2023
1 parent 9969254 commit bbc47e6
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Crawler/Export/SitemapExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ public function export(): void
$urls[] = $visitedUrl->url;
}
}

// sort $urls primary by number of dashes ASC, secondary alphabetically ASC
usort($urls, function ($a, $b) {
$aDashes = substr_count(rtrim($a, '/'), '/');
$bDashes = substr_count(rtrim($b, '/'), '/');
if ($aDashes === $bDashes) {
return strcmp($a, $b);
}
return $aDashes - $bDashes;
});

if ($this->outputSitemapXml) {
try {
$sitemapFile = $this->generateXmlSitemap($this->outputSitemapXml, $urls);
Expand Down

0 comments on commit bbc47e6

Please sign in to comment.