Added Sort to the Utility Block in List group.

This commit is contained in:
ruri
2019-05-09 13:16:53 +02:00
parent ea1d6fb168
commit a93230e1a7
3 changed files with 58 additions and 9 deletions
+39
View File
@@ -3,6 +3,7 @@ using RuriLib.Models;
using RuriLib.ViewModels;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Numerics;
@@ -52,6 +53,9 @@ namespace RuriLib
/// <summary>Joins a list into a single string, separating the elements with a separator.</summary>
Join,
/// <summary>Sorts a list alphabetically, in ascending or descending order.</summary>
Sort,
/// <summary>Concatenates two lists into a longer list variable.</summary>
Concat,
@@ -146,6 +150,14 @@ namespace RuriLib
/// <summary>The separator for joining a list.</summary>
public string Separator { get { return separator; } set { separator = value; OnPropertyChanged(); } }
private bool ascending = true;
/// <summary>Whether the sort should happen in ascending order.</summary>
public bool Ascending { get { return ascending; } set { ascending = value; OnPropertyChanged(); } }
private bool numeric = false;
/// <summary>Whether a list is made of numeric values.</summary>
public bool Numeric { get { return numeric; } set { numeric = value; OnPropertyChanged(); } }
private string secondListName = ""; // Zip
/// <summary>The name of the second list variable.</summary>
public string SecondListName { get { return secondListName; } set { secondListName = value; OnPropertyChanged(); } }
@@ -227,6 +239,11 @@ namespace RuriLib
Separator = LineParser.ParseLiteral(ref input, "Separator");
break;
case ListAction.Sort:
while (LineParser.Lookahead(ref input) == TokenType.Boolean)
LineParser.SetBool(ref input, this);
break;
case ListAction.Concat:
case ListAction.Zip:
case ListAction.Map:
@@ -321,6 +338,12 @@ namespace RuriLib
.Literal(Separator);
break;
case ListAction.Sort:
writer
.Boolean(Ascending, "Ascending")
.Boolean(Numeric, "Numeric");
break;
case ListAction.Concat:
case ListAction.Zip:
case ListAction.Map:
@@ -420,6 +443,22 @@ namespace RuriLib
data.Variables.Set(new CVar(variableName, string.Join(separator, list), isCapture));
break;
case ListAction.Sort:
var sorted = list.Select(e => e).ToList(); // Clone the list so we don't edit the original one
if (Numeric)
{
var nums = sorted.Select(e => double.Parse(e, CultureInfo.InvariantCulture)).ToList();
nums.Sort();
sorted = nums.Select(e => e.ToString()).ToList();
}
else
{
sorted.Sort();
}
if (!Ascending) sorted.Reverse();
data.Variables.Set(new CVar(variableName, sorted, isCapture));
break;
case ListAction.Concat:
data.Variables.Set(new CVar(variableName, list.Concat(list2).ToList(), isCapture));
break;