使用IComparable根据参数对对象的ArrayList进行排序

时间:2020-10-24 09:05:36

标签: c# sorting arraylist icomparable

我创建了一个小型测试程序,以获取有关C#中IComparable接口的经验。以后我需要将界面用于其他内容。

在程序中,我想根据薪水对客户的ArraList进行排序。执行代码时,我总是会收到“ System.InvalidOperationException ”异常。

我已经尝试过不同版本的代码,并检查了一些教程,总是得到相同的结果。

我的代码:

O(1)

2 个答案:

答案 0 :(得分:3)

您是否有特定原因想要使用ArrayList而不是List<Customer>?通常建议使用类型列表,因为它只接受指定类型的对象。

这里的问题是ArrayList.Sort()不使用IComparable界面。检查备注https://github.com/auth0/angular2-jwt,其中指出必须实现IComparer接口并使用.Sort(comparer)重载。

这是您的一种选择。另一个选择(容易得多)是使用List<Customer>,它将立即使用您的IComparable实现。您的选择:)

答案 1 :(得分:0)

这是使用原始ArrayList的解决方案

ERROR in src/app/postlist/postlist.page.html:7:78 - error TS2769: No overload matches this call.
  The last overload gave the following error.
    Argument of type 'Observable<any>' is not assignable to parameter of type 'Map<unknown, unknown>'.
      Type 'Observable<any>' is missing the following properties from type 'Map<unknown, unknown>': clear, delete, get, has, and 7 more.

7   <div *ngFor="let post of postlists | keyvalue">
                                                                               ~~~~~~~~~

  src/app/postlist/postlist.page.ts:8:16
    8   templateUrl: './postlist.page.html',
                     ~~~~~~~~~~~~~~~~~~~~~~
    Error occurs in the template of component PostlistPage.

[ERROR] An error occurred while running subprocess ng.