C++、Net、WindowsForms 按其他列表的值对列表进行排序
我有两个清单。其中一个具有 MyClass 类型,并且充满了我编写的这些类。第二个有一些 int 值。
问题是我不知道如何处理使用第二个列表的值对第一个列表进行排序。 我不知道如何编写 IComparer 来使用排序列表方法。我在msdn中看到了一个例子,但我不知道如何改变它。 http://msdn.microsoft.com/en-us/library/234b841s .aspx#Y535
这是我的程序:
//####################################
ref class MyClass{
private:
int x;
int y;
String^ name;
public:
MyClass(int x, int y, String^ name){
this->x = x;
this->y = y;
this->name = name;
}
...
};
//###### in some button which will initialize...:
List<MyClass^>^ listMyClass = gcnew List<MyClass^>(3);
listMyClass->Add(gcnew MyClass(3, 5, "class1"));
listMyClass->Add(gcnew MyClass(1, 2, "class2"));
listMyClass->Add(gcnew MyClass(6, 8, "class3"));
List<int>^ numbers = gcnew List<int>(3);
numbers->Add(2);
numbers->Add(4);
numbers->Add(1);
//##################################
这些 List 中的数字代表元素的某种状态的 listMyClass.所以我想使用列表编号中的值对 listMyClass 进行排序。 我希望排序后得到这样的结果: listMyClass[1]
是第一个,因为列表中的数字 4 是最高的。然后 listMyClass[0]
然后 listMyClass[2]
是否有可能应对这一挑战?
I have two lists. One has type of MyClass and is filled with these classes wrote by me. And the second one has some int values.
And the problem is that I don't know how to deal with sorting the first list using values of the second one.
I have no idea how to write an IComparer to use sort list method. I saw an example in msdn but I don't know how to change it.
http://msdn.microsoft.com/en-us/library/234b841s.aspx#Y535
Here's my program:
//##################################
ref class MyClass{
private:
int x;
int y;
String^ name;
public:
MyClass(int x, int y, String^ name){
this->x = x;
this->y = y;
this->name = name;
}
...
};
//###### in some button which will initialize...:
List<MyClass^>^ listMyClass = gcnew List<MyClass^>(3);
listMyClass->Add(gcnew MyClass(3, 5, "class1"));
listMyClass->Add(gcnew MyClass(1, 2, "class2"));
listMyClass->Add(gcnew MyClass(6, 8, "class3"));
List<int>^ numbers = gcnew List<int>(3);
numbers->Add(2);
numbers->Add(4);
numbers->Add(1);
//###################################
These numbers of numbers List represent some kind of a status of elements of listMyClass. So I'd like to sort listMyClass using values form the list numbers.
I would like to have such result after sorting:listMyClass[1]
is first because number 4 from the list numbers is the highest. ThenlistMyClass[0]
and then listMyClass[2]
Are there any posibilities to deal with this challange?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果第二个列表中是唯一的
int 值
,您可以使用std::vector
在您存储的 int 值指示的位置插入MyClass
联合国第二个清单。如果没有,请使用以int 值
作为键的std::multiset
。If are unique
int values
in second list, you may use astd::vector
for insertingMyClass
in the position indicated by int values you stored un second list. If not, use astd::multiset
withint values
as key.(并非特定于 C++/CLI)
使用
Dictionary
代替第二个列表:然后,您可以轻松实现自定义
IComparer.Compare(T x, T y)
,它将...字典
。Dictionary
中查找y.status
的排序顺序。...然后将此比较器传递给第一个列表的
List.Sort(IComparer)
。PS 对于本机 C++ 解决方案,您可以对
std::unordered_map
使用类似的想法。(not specific to C++/CLI)
Use
Dictionary<int, int>
in place of the second list:You can then easily implement a custom
IComparer<T>.Compare(T x, T y)
that would...x.status
from theDictionary
.y.status
from theDictionary
....then pass this comparer to
List<T>.Sort(IComparer<T>)
for the first list.P.S. For a native C++ solution, you could use a similar idea with
std::unordered_map<int, int>
.我成功地战胜了这个挑战:)
这是
我感到如释重负的解决方案:) 谢谢您的任何建议。
I managed to defeat this challange :)
Here's the solution
I felt such relief :) Thank You for any advices.