diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/InfoItemsCollector.java b/extractor/src/main/java/org/schabi/newpipe/extractor/InfoItemsCollector.java index 72eca16eb..1fd4b28a4 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/InfoItemsCollector.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/InfoItemsCollector.java @@ -3,8 +3,10 @@ package org.schabi.newpipe.extractor; import org.schabi.newpipe.extractor.exceptions.FoundAdException; import org.schabi.newpipe.extractor.exceptions.ParsingException; +import javax.annotation.Nullable; import java.util.ArrayList; import java.util.Collections; +import java.util.Comparator; import java.util.List; /* @@ -32,17 +34,31 @@ public abstract class InfoItemsCollector itemList = new ArrayList<>(); private final List errors = new ArrayList<>(); private final int serviceId; + @Nullable + private final Comparator comparator; + + /** + * Create a new collector with no comparator / sorting function + * @param serviceId the service id + */ + public InfoItemsCollector(final int serviceId) { + this(serviceId, null); + } /** * Create a new collector * @param serviceId the service id */ - public InfoItemsCollector(int serviceId) { + public InfoItemsCollector(final int serviceId, @Nullable final Comparator comparator) { this.serviceId = serviceId; + this.comparator = comparator; } @Override public List getItems() { + if (comparator != null) { + itemList.sort(comparator); + } return Collections.unmodifiableList(itemList); } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/media_ccc/extractors/MediaCCCRecentKiosk.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/media_ccc/extractors/MediaCCCRecentKiosk.java index 2656f7d64..6f95aa62f 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/media_ccc/extractors/MediaCCCRecentKiosk.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/media_ccc/extractors/MediaCCCRecentKiosk.java @@ -16,6 +16,7 @@ import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector; import javax.annotation.Nonnull; import java.io.IOException; +import java.util.Comparator; public class MediaCCCRecentKiosk extends KioskExtractor { @@ -40,7 +41,14 @@ public class MediaCCCRecentKiosk extends KioskExtractor { @Override public InfoItemsPage getInitialPage() throws IOException, ExtractionException { final JsonArray events = doc.getArray("events"); - StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId()); + + // Streams in the recent kiosk are not ordered by the release date. + // Sort them to have the latest stream at the beginning of the list. + Comparator comparator = Comparator.comparing( + streamInfoItem -> streamInfoItem.getUploadDate().offsetDateTime()); + comparator = comparator.reversed(); + + StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId(), comparator); for (int i = 0; i < events.size(); i++) { collector.commit(new MediaCCCRecentKioskExtractor(events.getObject(i))); } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfoItemsCollector.java b/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfoItemsCollector.java index b81052b74..da334310f 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfoItemsCollector.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfoItemsCollector.java @@ -5,6 +5,7 @@ import org.schabi.newpipe.extractor.InfoItemsCollector; import org.schabi.newpipe.extractor.exceptions.FoundAdException; import org.schabi.newpipe.extractor.exceptions.ParsingException; +import java.util.Comparator; import java.util.List; import java.util.Vector; @@ -34,6 +35,10 @@ public class StreamInfoItemsCollector extends InfoItemsCollector comparator) { + super(serviceId, comparator); + } + @Override public StreamInfoItem extract(StreamInfoItemExtractor extractor) throws ParsingException { if (extractor.isAd()) {