You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
165 lines
5.2 KiB
165 lines
5.2 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
|
|
namespace Apewer.Internals
|
|
{
|
|
|
|
internal class SortHelper
|
|
{
|
|
|
|
#region List<String>
|
|
|
|
public static List<string> ListString(List<string> argList, bool argAscend)
|
|
{
|
|
var valid = new List<string>();
|
|
|
|
var origin = argList;
|
|
if (origin == null) return valid;
|
|
if (origin.Count < 2)
|
|
{
|
|
if (origin.Count == 1) valid.Add(origin[0]);
|
|
return valid;
|
|
}
|
|
|
|
foreach (var item in origin)
|
|
{
|
|
if (string.IsNullOrEmpty(item)) continue;
|
|
var trim = item.Trim();
|
|
if (string.IsNullOrEmpty(trim)) continue;
|
|
valid.Add(trim);
|
|
}
|
|
|
|
if (argAscend) valid.Sort(new Comparison<string>(ComparisonAscend));
|
|
else valid.Sort(new Comparison<string>(ComparisonDescend));
|
|
|
|
return valid;
|
|
}
|
|
|
|
public static int ComparisonAscend(string x, string y)
|
|
{
|
|
return x.CompareTo(y);
|
|
}
|
|
|
|
public static int ComparisonDescend(string x, string y)
|
|
{
|
|
return y.CompareTo(x);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Dictionary<String, String>
|
|
|
|
public static Dictionary<string, string> DictionaryStringString(Dictionary<string, string> argDictionary, bool argKey, bool argAscend)
|
|
{
|
|
var input = argDictionary;
|
|
var list = new List<KeyValuePair<string, string>>(input);
|
|
if (list.Count > 0)
|
|
{
|
|
try
|
|
{
|
|
if (argKey)
|
|
{
|
|
if (argAscend) list.Sort(new Comparison<KeyValuePair<string, string>>(ComparisonAscendKey));
|
|
else list.Sort(new Comparison<KeyValuePair<string, string>>(ComparisonDescendKey));
|
|
}
|
|
else
|
|
{
|
|
if (argAscend) list.Sort(new Comparison<KeyValuePair<string, string>>(ComparisonAscendValue));
|
|
else list.Sort(new Comparison<KeyValuePair<string, string>>(ComparisonDescendValue));
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
list.Clear();
|
|
}
|
|
}
|
|
var output = new Dictionary<string, string>();
|
|
foreach (var kvp in list)
|
|
{
|
|
output.Add(kvp.Key, kvp.Value);
|
|
}
|
|
return output;
|
|
}
|
|
|
|
public static int ComparisonAscendKey(KeyValuePair<string, string> x, KeyValuePair<string, string> y)
|
|
{
|
|
return x.Key.CompareTo(y.Key);
|
|
}
|
|
|
|
public static int ComparisonDescendKey(KeyValuePair<string, string> x, KeyValuePair<string, string> y)
|
|
{
|
|
return y.Key.CompareTo(x.Key);
|
|
}
|
|
|
|
public static int ComparisonAscendValue(KeyValuePair<string, string> x, KeyValuePair<string, string> y)
|
|
{
|
|
return x.Value.CompareTo(y.Value);
|
|
}
|
|
|
|
public static int ComparisonDescendValue(KeyValuePair<string, string> x, KeyValuePair<string, string> y)
|
|
{
|
|
return y.Value.CompareTo(x.Value);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Dictionary<String, Double>
|
|
|
|
public static Dictionary<string, double> DictionaryStringDouble(Dictionary<string, double> argDictionary, bool argKey, bool argAscend)
|
|
{
|
|
var input = argDictionary;
|
|
var list = new List<KeyValuePair<string, double>>(input);
|
|
if (list.Count > 0)
|
|
{
|
|
try
|
|
{
|
|
if (argKey)
|
|
{
|
|
if (argAscend) list.Sort(new Comparison<KeyValuePair<string, double>>(ComparisonAscendKey));
|
|
else list.Sort(new Comparison<KeyValuePair<string, double>>(ComparisonDescendKey));
|
|
}
|
|
else
|
|
{
|
|
if (argAscend) list.Sort(new Comparison<KeyValuePair<string, double>>(ComparisonAscendValue));
|
|
else list.Sort(new Comparison<KeyValuePair<string, double>>(ComparisonDescendValue));
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
list.Clear();
|
|
}
|
|
}
|
|
var output = new Dictionary<string, double>();
|
|
foreach (var kvp in list)
|
|
{
|
|
output.Add(kvp.Key, kvp.Value);
|
|
}
|
|
return output;
|
|
}
|
|
|
|
public static int ComparisonAscendKey(KeyValuePair<string, double> x, KeyValuePair<string, double> y)
|
|
{
|
|
return x.Key.CompareTo(y.Key);
|
|
}
|
|
|
|
public static int ComparisonDescendKey(KeyValuePair<string, double> x, KeyValuePair<string, double> y)
|
|
{
|
|
return y.Key.CompareTo(x.Key);
|
|
}
|
|
|
|
public static int ComparisonAscendValue(KeyValuePair<string, double> x, KeyValuePair<string, double> y)
|
|
{
|
|
return x.Value.CompareTo(y.Value);
|
|
}
|
|
|
|
public static int ComparisonDescendValue(KeyValuePair<string, double> x, KeyValuePair<string, double> y)
|
|
{
|
|
return y.Value.CompareTo(x.Value);
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|
|
|