C++、Net、WindowsForms 按其他列表的值对列表进行排序

发布于 2024-12-08 02:44:54 字数 1228 浏览 1 评论 0原文

我有两个清单。其中一个具有 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. Then
listMyClass[0] and then listMyClass[2]

Are there any posibilities to deal with this challange?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

掩于岁月 2024-12-15 02:44:54

如果第二个列表中是唯一的 int 值,您可以使用 std::vector 在您存储的 int 值指示的位置插入 MyClass联合国第二个清单。如果没有,请使用以 int 值 作为键的 std::multiset

If are unique int values in second list, you may use a std::vector for inserting MyClassin the position indicated by int values you stored un second list. If not, use a std::multiset with int values as key.

靑春怀旧 2024-12-15 02:44:54

(并非特定于 C++/CLI)

使用 Dictionary 代替第二个列表:

  • 键是状态。
  • 值是该状态的排序顺序

然后,您可以轻松实现自定义 IComparer.Compare(T x, T y),它将...

  1. 字典
  2. Dictionary 中查找y.status 的排序顺序。
  3. 直接比较这些排序顺序。

...然后将此比较器传递给第一个列表的 List.Sort(IComparer)

PS 对于本机 C++ 解决方案,您可以对 std::unordered_map 使用类似的想法。

(not specific to C++/CLI)

Use Dictionary<int, int> in place of the second list:

  • Key is the status.
  • Value is the sort order of that status.

You can then easily implement a custom IComparer<T>.Compare(T x, T y) that would...

  1. Lookup the sort order of the x.status from the Dictionary.
  2. Lookup the sort order of the y.status from the Dictionary.
  3. Compare these sort orders directly.

...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>.

人疚 2024-12-15 02:44:54

我成功地战胜了这个挑战:)
这是

ref class MyClass:IComparable<MyClass^>{
public:
  int x;
  int y;
  int status;
  String^ name;

  MyClass(int x, int y, String^ name){
  this->x = x;
  this->y = y;
  this->name = name;
  }

  virtual int CompareTo(MyClass^ tmpMyClass){
      if(this->status > tmpMyClass->status)
         return 1;
      if(this->status < tmpMyClass->status)
         return -1;
      else
         return 0;
  }
...
};

//###### 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);

for(int i=0; i<listMyClass->Count; i++){
   listMyClass->status = numbers[i];
}
listMyClass->Sort();

我感到如释重负的解决方案:) 谢谢您的任何建议。

I managed to defeat this challange :)
Here's the solution

ref class MyClass:IComparable<MyClass^>{
public:
  int x;
  int y;
  int status;
  String^ name;

  MyClass(int x, int y, String^ name){
  this->x = x;
  this->y = y;
  this->name = name;
  }

  virtual int CompareTo(MyClass^ tmpMyClass){
      if(this->status > tmpMyClass->status)
         return 1;
      if(this->status < tmpMyClass->status)
         return -1;
      else
         return 0;
  }
...
};

//###### 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);

for(int i=0; i<listMyClass->Count; i++){
   listMyClass->status = numbers[i];
}
listMyClass->Sort();

I felt such relief :) Thank You for any advices.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文