-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Implement sort logic for winget list output #6191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Madhusudhan-MSFT
wants to merge
16
commits into
microsoft:master
Choose a base branch
from
Madhusudhan-MSFT:user/masudars/list-sort-logic
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
49ae654
[ListSort, Workflow] Add sort logic for winget list output
Madhusudhan-MSFT a210423
[ListSortTests, Refactor] Extract sort logic into testable header and…
Madhusudhan-MSFT 918fe25
[ListSortHelper, Refactor] Move sort implementations from header to c…
Madhusudhan-MSFT 7469608
[ListSort, Doc] Update list command documentation with sort options
Madhusudhan-MSFT 6f7773f
[ListSort, Validation] Add argument validation for --sort values
Madhusudhan-MSFT 813c8af
[ListSort, SpellCheck] Add listsort to spelling expect list
Madhusudhan-MSFT a32a676
[ListSort, Design] Respect settings sort with queries per reviewer fe…
Madhusudhan-MSFT 46a5707
[ListSort, Fix] Code correctness per reviewer feedback
Madhusudhan-MSFT dc65788
[ListSort, Design] Default sort by name when no settings or query
Madhusudhan-MSFT ea81abd
[ListSort, Refactor] Redesign helper as production sort pipeline
Madhusudhan-MSFT 0d86ce9
[ListSort, Rename] Rename ListSortHelper to PackageTableSortHelper
Madhusudhan-MSFT aaf8e9c
[ListSort, Optimize] Skip unused field computation via bitmask
Madhusudhan-MSFT 8ac341d
[ListSort, Polish] Compile-time drift guard, code cleanup, and docs
Madhusudhan-MSFT c19ce84
[ListSort] Address PR review feedback for sort implementation
Madhusudhan-MSFT 4065b64
[ListSort] Address round 2 PR review feedback
Madhusudhan-MSFT dc2542d
[ListSort, Refactor] Consolidate sort parameter resolution into SortP…
Madhusudhan-MSFT File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -290,6 +290,7 @@ LEBOM | |
| lhs | ||
| LIBYAML | ||
| liv | ||
| listsort | ||
| liwpx | ||
| localizationpriority | ||
| localsource | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
172 changes: 172 additions & 0 deletions
172
src/AppInstallerCLICore/Workflows/PackageTableSortHelper.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
| #include "pch.h" | ||
| #include "PackageTableSortHelper.h" | ||
| #include "ExecutionContext.h" | ||
|
|
||
| namespace AppInstaller::CLI::Workflow | ||
| { | ||
| using namespace Settings; | ||
|
|
||
| SortablePackageEntry::SortablePackageEntry( | ||
| size_t originalIndex, | ||
| std::string_view name, | ||
| std::string_view id, | ||
| std::string_view installedVersion, | ||
| std::string_view availableVersion, | ||
| std::string_view source, | ||
| SortField fieldMask) | ||
| : OriginalIndex(originalIndex) | ||
| { | ||
| if (WI_IsFlagSet(fieldMask, SortField::Name)) | ||
| { | ||
| FoldedName = Utility::FoldCase(name); | ||
| } | ||
| if (WI_IsFlagSet(fieldMask, SortField::Id)) | ||
| { | ||
| FoldedId = Utility::FoldCase(id); | ||
| } | ||
| if (WI_IsFlagSet(fieldMask, SortField::Source)) | ||
| { | ||
| FoldedSource = Utility::FoldCase(source); | ||
| } | ||
| if (WI_IsFlagSet(fieldMask, SortField::Version)) | ||
| { | ||
| ParsedInstalledVersion = Utility::Version{ std::string{ installedVersion } }; | ||
| } | ||
| if (WI_IsFlagSet(fieldMask, SortField::Available)) | ||
| { | ||
| if (!availableVersion.empty()) | ||
| { | ||
| ParsedAvailableVersion = Utility::Version{ std::string{ availableVersion } }; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| SortField ComputeSortFieldMask(const std::vector<SortField>& sortFields) | ||
| { | ||
| SortField mask = SortField::None; | ||
| for (const auto& f : sortFields) | ||
| { | ||
| mask |= f; | ||
| } | ||
| return mask; | ||
| } | ||
|
|
||
| SortParameters::SortParameters(const Execution::Context& context) | ||
| { | ||
| if (context.Args.Contains(Execution::Args::Type::Sort)) | ||
| { | ||
| for (const auto& arg : *context.Args.GetArgs(Execution::Args::Type::Sort)) | ||
| { | ||
| auto field = ConvertToSortField(arg); | ||
| WI_ASSERT(field.has_value()); | ||
| if (field.has_value()) | ||
| { | ||
| Fields.emplace_back(field.value()); | ||
| } | ||
| } | ||
| } | ||
| else | ||
| { | ||
| Fields = User().Get<Setting::OutputSortOrder>(); | ||
|
|
||
| if (Fields.empty()) | ||
| { | ||
| bool hasQuery = context.Args.Contains(Execution::Args::Type::Query) || | ||
| context.Args.Contains(Execution::Args::Type::MultiQuery); | ||
|
|
||
| if (hasQuery) | ||
| { | ||
| // Preserve relevance ordering when a free-text query is present | ||
| // and no explicit sort preference is configured. | ||
| return; // ShouldSort remains false | ||
| } | ||
|
|
||
| // No settings, no query — default to name sort. | ||
| Fields.emplace_back(SortField::Name); | ||
| } | ||
| } | ||
|
|
||
| // Relevance-only means preserve source ordering. | ||
| if (Fields.size() == 1 && Fields[0] == SortField::Relevance) | ||
| { | ||
| return; // ShouldSort remains false | ||
| } | ||
|
|
||
| // Resolve direction: CLI flags override settings | ||
| Direction = context.Args.Contains(Execution::Args::Type::SortDescending) ? SortDirection::Descending : | ||
| context.Args.Contains(Execution::Args::Type::SortAscending) ? SortDirection::Ascending : | ||
| User().Get<Setting::OutputSortDirection>(); | ||
|
|
||
| ShouldSort = true; | ||
| } | ||
|
|
||
| int CompareByField(const SortablePackageEntry& a, const SortablePackageEntry& b, SortField field) | ||
| { | ||
| switch (field) | ||
| { | ||
| case SortField::Name: | ||
| return a.FoldedName.compare(b.FoldedName); | ||
| case SortField::Id: | ||
| return a.FoldedId.compare(b.FoldedId); | ||
| case SortField::Version: | ||
| { | ||
| if (a.ParsedInstalledVersion < b.ParsedInstalledVersion) return -1; | ||
| if (b.ParsedInstalledVersion < a.ParsedInstalledVersion) return 1; | ||
| return 0; | ||
| } | ||
| case SortField::Source: | ||
| return a.FoldedSource.compare(b.FoldedSource); | ||
| case SortField::Available: | ||
| { | ||
| bool aHas = a.ParsedAvailableVersion.has_value(); | ||
| bool bHas = b.ParsedAvailableVersion.has_value(); | ||
|
|
||
| // Has-version sorts before no-version in ascending order. | ||
| if (aHas != bHas) | ||
| { | ||
| return aHas ? -1 : 1; | ||
| } | ||
|
|
||
| // Both have versions — compare normally. | ||
| if (aHas && bHas) | ||
| { | ||
| if (a.ParsedAvailableVersion.value() < b.ParsedAvailableVersion.value()) return -1; | ||
| if (b.ParsedAvailableVersion.value() < a.ParsedAvailableVersion.value()) return 1; | ||
| } | ||
| return 0; | ||
| } | ||
| case SortField::Relevance: | ||
| // Relevance has no precomputed sort key — preserve original ordering. | ||
| return 0; | ||
| default: | ||
| WI_ASSERT(false); | ||
| return 0; | ||
| } | ||
| } | ||
|
|
||
| void SortEntries( | ||
| std::vector<SortablePackageEntry>& entries, | ||
| const SortParameters& sortParams) | ||
| { | ||
| if (entries.size() <= 1 || !sortParams.ShouldSort) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| std::stable_sort(entries.begin(), entries.end(), | ||
| [&sortParams](const SortablePackageEntry& a, const SortablePackageEntry& b) | ||
| { | ||
| for (const auto& field : sortParams.Fields) | ||
| { | ||
| int cmp = CompareByField(a, b, field); | ||
| if (cmp != 0) | ||
| { | ||
| return sortParams.Direction == SortDirection::Ascending ? (cmp < 0) : (cmp > 0); | ||
| } | ||
| } | ||
| return false; | ||
| }); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.