任何人都可以帮我改变基于特定值的数组顺序,即,如果我的数组= [0,1,2,3,4,5,6]并且我想重新排列数组值,如果有一些值像5那么数组应该像[5,6,0,1,2,3,4]那样,如果我有一个值4那么数组应该从[4,5,6,0,1,2]开始,3]
string[] GetDays; //"Here i will get value from User"
string value; //"This value will also come from User but will always be present in above Array"
if(GetDays.Find(value))
{
//Rearrange the sort starting with value in ascending
}
注意:数组元素不是常量,所以我们必须以编程方式排列顺序,因为某些时间数组可以是[1,3,5,6],如果我的值为5,那么我的数组应该像[5, 6,1,3]。 感谢
答案 0 :(得分:0)
将Array.Sort与Comparer一起使用:像这样:
var a=new []{1,2,3,4,5,6};
var v=5;
//Create IComparer
Comparison<int> comparison = (x,y) => (x >= v && y < v)
? -1 // Case x is over user value but y is not: x ordered before y
: x.CompareTo(y); // otherwise standard comparison;
Array.Sort(a, comparison);
答案 1 :(得分:0)
这可以通过几行来实现:
int[] test = {0, 1, 2, 3, 4, 5, 6};
var userValue = 3;
var sorted = test.GroupBy(i => i < userValue).OrderBy(i => i.Key)
.Select(i => i.OrderBy(j => j)).SelectMany(i => i).ToArray();
输出:
{3,4,5,6,0,1,2}
答案 2 :(得分:0)
string[] GetDays = {"s","t","a","c","k"};
string [] temparray = new string[GetDays.Length];
string value = "c";
int index = Array.IndexOf<string>(GetDays, value);
for(int i=0;i<GetDays.Length;i++)
{
temparray[i] = GetDays[inde];
index = index+1>=GetDays.Length?0:index+1;
}
temparray
将包含所需的输出。