Malarg commented on code in PR #25397:
URL: https://github.com/apache/beam/pull/25397#discussion_r1109398110
##########
playground/frontend/lib/pages/standalone_playground/notifiers/example_selector_state.dart:
##########
@@ -34,6 +35,7 @@ class ExampleSelectorState with ChangeNotifier {
this._searchText = '',
]) {
tags = _getTagsSortedByExampleCount(categories);
Review Comment:
not at all, simplified
##########
playground/frontend/lib/pages/standalone_playground/notifiers/example_selector_state.dart:
##########
@@ -47,28 +49,61 @@ class ExampleSelectorState with ChangeNotifier {
void addSelectedTag(String tag) {
selectedTags.add(tag);
+ _sortTagsBySelected();
notifyListeners();
}
void removeSelectedTag(String tag) {
selectedTags.remove(tag);
+ _sortTagsBySelected();
notifyListeners();
}
+ void _sortTagsBySelected() {
Review Comment:
done
##########
playground/frontend/lib/pages/standalone_playground/notifiers/example_selector_state.dart:
##########
@@ -47,28 +49,61 @@ class ExampleSelectorState with ChangeNotifier {
void addSelectedTag(String tag) {
selectedTags.add(tag);
+ _sortTagsBySelected();
notifyListeners();
}
void removeSelectedTag(String tag) {
selectedTags.remove(tag);
+ _sortTagsBySelected();
notifyListeners();
}
+ void _sortTagsBySelected() {
+ tags.sort((a, b) {
+ if (selectedTags.contains(a) && !selectedTags.contains(b)) {
+ return -1;
+ } else if (!selectedTags.contains(a) && selectedTags.contains(b)) {
+ return 1;
+ } else {
+ final aFreq = tagsFrequencyMap[a] ?? -1;
+ final bFreq = tagsFrequencyMap[b] ?? -1;
+ if (aFreq > bFreq) {
+ return -1;
+ } else if (aFreq < bFreq) {
+ return 1;
+ } else {
+ return a.compareTo(b);
+ }
+ }
+ });
+ }
+
List<String> _getTagsSortedByExampleCount(
List<CategoryWithExamples> categories,
) {
- Map<String, int> exampleCountByTag = {};
+ Map<String, int> tagsFrequencyMap = _buildTagsFrequencyMap(categories);
+ final tagEntries = tagsFrequencyMap.entries.toList()
+ ..sort(
+ (entry1, entry2) => entry2.value.compareTo(entry1.value) != 0
+ ? entry2.value.compareTo(entry1.value)
+ : entry2.key.compareTo(entry1.key),
+ );
+ return tagEntries.map((entry) => entry.key).toList();
+ }
+
+ Map<String, int> _buildTagsFrequencyMap(
+ List<CategoryWithExamples> categories,
+ ) {
+ Map<String, int> result = {};
Review Comment:
done
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]