Vb.net / C# - 从中点开始排序 list/array
Vb.net / C# - sort list/array starting from a mid point
有没有简单的方法从特定值开始对列表进行排序?
A B C D E F
所以我们可以从传入的值开始说 'C'
C D E F A B
我想优化一个时间段列表,最好从开始时间开始排序,这样就不需要一直迭代。
您可以使用:
ArrayList.Sort(int index, int count, IEqualityComparer comparer)
此方法使用指定的比较器对 ArrayList 中的元素范围内的元素进行排序。
Parameters
index
Type: System.Int32 The zero-based starting index of the range to sort.
count
Type: System.Int32
The length of the range to sort.
comparer
Type: System.Collections.IComparer The IComparer implementation to use
when comparing elements.
-or-
A null reference (Nothing in Visual Basic) to use the IComparable
implementation of each element.
这里有一个例子解释了如何使用它。
如果您不知道如何实现相等比较器,您也可以查看此
更新:
来自您的数组:A B C D E F
假设它在这个对象中并且被实例化:
List<string> array;
所以你可以这样做:
public List<string> foo(List<string> array, int i) {
array.Sort(i, array.size() - i, null);
List<string> aux = array.Skip(i).Take(array.Size() - i );
aux.AddRange(array.Take(i));
return aux;
}
你这样称呼它:
List<string myNewArray = foo(array, 2);
static void Main(string[] args)
{
char[] z = new char[] { 'B', 'C', 'F', 'A', 'D', 'E' };
Array.Sort(z);
char[] x,y;
Split<char>(z, 2, out x, out y);
char[] combined = y.Concat(x).ToArray ();
}
public static void Split<T>(T[] array, int index, out T[] first, out T[] second)
{
first = array.Take(index).ToArray();
second = array.Skip(index).ToArray();
}
有没有简单的方法从特定值开始对列表进行排序?
A B C D E F
所以我们可以从传入的值开始说 'C'
C D E F A B
我想优化一个时间段列表,最好从开始时间开始排序,这样就不需要一直迭代。
您可以使用:
ArrayList.Sort(int index, int count, IEqualityComparer comparer)
此方法使用指定的比较器对 ArrayList 中的元素范围内的元素进行排序。
Parameters
index
Type: System.Int32 The zero-based starting index of the range to sort.
count
Type: System.Int32
The length of the range to sort.
comparer
Type: System.Collections.IComparer The IComparer implementation to use when comparing elements.
-or-
A null reference (Nothing in Visual Basic) to use the IComparable implementation of each element.
这里有一个例子解释了如何使用它。
如果您不知道如何实现相等比较器,您也可以查看此
更新:
来自您的数组:A B C D E F
假设它在这个对象中并且被实例化:
List<string> array;
所以你可以这样做:
public List<string> foo(List<string> array, int i) {
array.Sort(i, array.size() - i, null);
List<string> aux = array.Skip(i).Take(array.Size() - i );
aux.AddRange(array.Take(i));
return aux;
}
你这样称呼它:
List<string myNewArray = foo(array, 2);
static void Main(string[] args)
{
char[] z = new char[] { 'B', 'C', 'F', 'A', 'D', 'E' };
Array.Sort(z);
char[] x,y;
Split<char>(z, 2, out x, out y);
char[] combined = y.Concat(x).ToArray ();
}
public static void Split<T>(T[] array, int index, out T[] first, out T[] second)
{
first = array.Take(index).ToArray();
second = array.Skip(index).ToArray();
}