矢量排序,我們按照另一個順序排列一個矢量?像這樣?
// first, our base data
Tuple<char,char>[] data = { new Tuple<char,char>('g','d') ,
new Tuple<char,char>('s','e') ,
new Tuple<char,char>('d','s') ,
new Tuple<char,char>('e','g') ,
} ;
// initialize the two vectors
List<char> keys = new List<char>(data.Select(x => x.Item1)) ; // {g,d,s,e}
List<Tuple<char,char>> values = new List<Tuple<char, char>>(data) ;
// An in-place sort of the "keys" vector in terms of the payload of the "values" vector
keys.Sort((x,y) => {
// first, find the associated value for each key (if such exists)
char? xValue = null ;
char? yValue = null ;
foreach (Tuple<char, char> t in values)
{
if (t.Item1 == x && !xValue.HasValue) xValue = t.Item2 ;
if (t.Item1 == y && !yValue.HasValue) yValue = t.Item2 ;
// if we found both values, we can quit
if (xValue.HasValue && yValue.HasValue ) break ;
}
// set the condition (comparison code)
int cc ;
if (xValue.HasValue && yValue.HasValue) cc = xValue.Value.CompareTo(yValue.Value) ;
else if (xValue.HasValue ) cc = +1 ; // treat NULL as collating lower than non-null
else if ( yValue.HasValue) cc = -1 ; // treat NULL as collating lower than non-null
else cc = 0 ; // equal if both are NULL
// return it
return cc ;
});
用什麼語言?根據列表2進行排序? – herohuyongtao
不清楚你想要什麼。舉個例子。排序後,你希望列表1看起來像什麼樣子。 –
您是否在兩個容器中都有固定數量的元素? – user1767754