我有一个交换两个项目的方法:
swap( collection['a'], collection['b'] ); // result = 'b', 'a'
swap( collection[0], collection[1] ); // result = collection[1], collection[0]
swap( 0, collection.indexOf(collection[1]) ); // result collection[1] index, 0
请帮我实现这个算法。
谢谢!
对于那些关心的人来说,这不是家庭作业。
示例:
//Example 1:
//collection contains: 'a', 'b', 'c', 'd'
//desired order: 'd', 'b', 'a', 'c'
swap(0, collection.IndexOf(collection['d']));
swap(1, collection.IndexOf(collection['b']));
swap(2, collection.IndexOf(collection['a']));
swap(3, collection.IndexOf(collection['c']));
//Example 2:
//collection contains: 'a', 'b', 'c'
//desired order: 'b', 'a', 'c'
swap(0, collection.IndexOf(collection['b']));
swap(1, collection.IndexOf(collection['a']));
swap(2, collection.IndexOf(collection['c']));
答案 0 :(得分:2)
基本上,您正在寻找间接比较的排序。即,不是比较字母本身,而是比较它们在表格中查找的值。如果您原谅C ++语法,一般的想法是这样的:
class my_cmp {
static const int positions[] = { 2, 1, 3, 0};
public:
bool operator<(char a, char b) {
return positions[a-'a'] < positions[b-'a'];
}
}:
std::sort(collection.begin(), collection.end(), my_cmp());
std::sort
将使用swap
移动集合中的元素。虽然语法显然会有所不同,但从我上次使用它时记得的情况来看,同样的一般想法也应该适用于C#。
答案 1 :(得分:2)
Jerry的C ++解决方案适用于C#:
using System;
using IComparer = System.Collections.IComparer;
class CustomOrder: IComparer
{
static readonly int[] positions = { 2, 1, 3, 0 };
public int Compare( object x, object y )
{
return positions[(char)x-'a'].CompareTo( positions[(char)y-'a'] );
}
}
class Startup
{
static void Main(string[] args)
{
char[] collection = {'a', 'b', 'c', 'd'};
Console.WriteLine( collection ); // abcd
Array.Sort( collection, new CustomOrder() );
Console.WriteLine( collection ); // dbac
}
}
答案 2 :(得分:0)
我现在意识到(感谢那些回答的人),使用下面这样的自定义IComparer消除了使用交换的需要。此解决方案保证了订单,并且在5中的一个时仍能正常工作缺少可能的值。
class CustomOrder : IComparer<Series>
{
static readonly Dictionary<string, int> dictionary =
new Dictionary<string, int>()
{
{"Excellent", 1},
{"Very Good", 2},
{"Average", 3},
{"Bad", 4},
{"Very Bad", 5}
};
public int Compare(Series x, Series y)
{
return dictionary[x.Name].CompareTo(dictionary[y.Name]);
}
}
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Series[] sortedSeries = chart.Series.ToArray();
Array.Sort(sortedSeries, new CustomOrder());
}
}
旧解决方案使用交换(供参考)
我不喜欢它,但这似乎有效。谁有更好的想法?
int count = collection.Count;
// don't need to swap if there is only one element
if (count > 1)
{
// have to run once for each letter
sortCollection(count);
sortCollection(count);
sortCollection(count);
sortCollection(count);
}
private void sortCollection(int count)
{
if (collection.Contains(collection['c']))
{
// take care of last element
collection.Swap(count - 1, collection.IndexOf(collection['c']));
}
if (collection.Contains(collection['a']) && collection.Contains(collection['b']))
{
// take care of middle elements
if(collection[1] != collection['b'])
collection.Swap(collection['a'], collection['b']);
}
if (collection.Contains(collection['d']))
{
// take care of first element
if(collection[0] != collection['d'])
collection.Swap(0, collection.IndexOf(collection['d']));
}
}