虽然我认为其他两个答案都是正确的,但我假设您不熟悉他们使用的 .NET 2.0 和 .NET 3.5 的一些功能。让我们一步一步来。
因此,您将获得一个 ArrayList,其中包含以下数据:
{ "1:1", "0:0", "0:1", "2:1", "1:0", "2:0" }
首先,使用 RegEx 没有任何问题;也许是轻微的性能损失。如果字符串真的这么简单,你可以使用Split:
string[] s = myArray[i].Split(new[] { ':' });
int val1 = int.Parse(s[0]);
int val2 = int.Parse(s[1]);
但是,由于您说您使用的是 .NET 4,因此您根本不应该使用 ArrayList - 请注意,它要求您将值转换为适当的类型,例如string mystring = myArray[i] as string.
有很多很棒的功能您没有利用,例如泛型(自 2.0 起在 .NET Framework 中)。让我们编写一个函数,给定一个ArrayList,但返回一个已排序的通用List<string>(一个只包含字符串的列表)。让我们看看:
/// <summary>
/// This method takes in an ArrayList of unsorted numbers in the format: a:b
/// and returns a sorted List<string> with a descending, b ascending
/// <summary>
public List<string> SortMyValues(ArrayList unsorted)
{
// Declare an empty, generic List of type 'TwoNumbers'
List<MyTuple> values = new List<MyTuple>();
foreach (object item in unsorted)
{
char[] splitChar = new char[] { ':' };
string itemString = item as string;
string[] s = itemString.Split(splitChar);
values.Add(new MyTuple{
FirstNumber = int.Parse(s[0]),
SecondNumber = int.Parse(s[1])
});
}
// Sort the values
values.Sort();
// Return a list of strings, in the format given
List<string> sorted = new List<string>();
foreach (MyTuple item in values)
{
sorted.Add(item.FirstNumber + ":" + item.SecondNumber);
}
return sorted;
}
public class MyTuple : IComparable {
public int FirstNumber { get; set; }
public int SecondNumber { get; set; }
public int CompareTo(object obj)
{
if (obj is MyTuple)
{
MyTuple other = (MyTuple)obj;
// First number descending
if (FirstNumber != other.FirstNumber)
return other.FirstNumber.CompareTo(FirstNumber);
// Second number ascending
return SecondNumber.CompareTo(other.SecondNumber);
}
throw new ArgumentException("object is not a MyTuple");
}
}
现在,上面的代码可以工作了,但是真的很长。请注意,您必须创建一个类来保存这两个值,使该类实现 IComparable 等。非常烦人!
.NET 3.5 推出了一些很棒的功能,包括 匿名类型 和 LINQ。让我们更改代码以使用这两个功能。
/// <summary>
/// This method takes in an ArrayList of unsorted numbers in the format: a:b
/// and returns a sorted List<string> with a descending, b ascending
/// <summary>
public List<string> SortMyValues(ArrayList unsorted)
{
// First, cast every single element of the given ArrayList to a string
// The Cast<T> method will do this, and return an enumerable collection
return unsorted.Cast<string>()
// Now, let's take this string data and create our objects that will hold two numbers
.Select(item => {
// This is the body of an anonymous method with one parameter, which I called 'item'
// This anonymous method will be executed for every element in the collection
string[] s = item.Split(new[] { ':' });
// Here we create a new anonymous type holding our numbers
// No need to define a new dummy class!
return new {
FirstNumber = int.Parse(s[0]),
SecondNumber = int.Parse(s[1])
};
})
// Now that we've got our objects, let's sort them
.OrderByDescending(x => x.FirstNumber)
.ThenBy(x => x.SecondNumber)
// Finally, now that we're sorted properly, let's format our string list output
.Select(x => x.FirstNumber + ":" + x.SecondNumber)
.ToList();
}
现在我们整个函数只有一行代码,大部分代码都是cmets。我鼓励您了解并开始使用其中一些功能;它会让你的代码更容易阅读和编写;-)
希望这有帮助!
编辑:回应您的评论:
按以下顺序排列它们需要什么:2:0 1:0 0:0 2:1 1:1 0:1
您似乎是按第二个数字升序排序,然后按第一个数字降序排序。
只需更改上面的代码即可使用:
.OrderBy(x => x.SecondNumber)
.ThenByDescending(x => x.FirstNumber)