Skip to content
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

Add natural string sorting to solve Issue #18 #19

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion PackageBuilder/Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Net.Http.Headers;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using ICSharpCode.SharpZipLib.Zip;
using Newtonsoft.Json;
Expand All @@ -19,6 +20,29 @@

namespace VRC.PackageManagement.Automation
{
// Pads numbers with `0` to the length of the longest number in the strings
// Using Regex is also fast and avoids interop with shlwapi.dll
public static class ListExtensions
{
public static IOrderedEnumerable<T> OrderByAlphaNumeric<T>(this IEnumerable<T> source, Func<T, string> selector)
{
int max = source
.SelectMany(i => Regex.Matches(selector(i), @"\d+").Cast<Match>().Select(m => (int?)m.Value.Length))
.Max() ?? 0;

return source.OrderBy(i => Regex.Replace(selector(i), @"\d+", m => m.Value.PadLeft(max, '0')));
}

public static IOrderedEnumerable<T> OrderByAlphaNumericDescending<T>(this IEnumerable<T> source, Func<T, string> selector)
{
int max = source
.SelectMany(i => Regex.Matches(selector(i), @"\d+").Cast<Match>().Select(m => (int?)m.Value.Length))
.Max() ?? 0;

return source.OrderByDescending(i => Regex.Replace(selector(i), @"\d+", m => m.Value.PadLeft(max, '0')));
}
}

[GitHubActions(
"GHTest",
GitHubActionsImage.UbuntuLatest,
Expand Down Expand Up @@ -234,7 +258,7 @@ ListingSource MakeListingSourceFromManifest(VRCPackageManifest manifest)

Serilog.Log.Information($"Made listingInfo {JsonConvert.SerializeObject(listingInfo, JsonWriteOptions)}");

var latestPackages = packages.OrderByDescending(p => p.Version).DistinctBy(p => p.Id).ToList();
var latestPackages = packages.OrderByAlphaNumericDescending(p => p.Version).DistinctBy(p => p.Id).ToList();
Serilog.Log.Information($"LatestPackages: {JsonConvert.SerializeObject(latestPackages, JsonWriteOptions)}");
var formattedPackages = latestPackages.ConvertAll(p => new {
Name = p.Id,
Expand Down