自定义排序 [英] Custom Sorting

查看:83
本文介绍了自定义排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我已将员工姓名存储在List< Feature>中.
我已经实现了IComparer接口的比较方法",以实现如下的memenet排序.

In My application, I have stored employ names in a List<Feature>.
I have implemented Compare Method of IComparer interface to implemenet sorting as follows.

public int Compare(firstEmPName, secondEmpName)
        {
            int diffResult = 0;
diffResult = string.Compare(firstEmPName, secondEmpName);
            return diffResult;
        } 



我已经显示了简单的示例,但实际排序非常复杂.因此,我使用了IComparer.

现在我想在排序过程中保留一些名称(例如:nameFirst和nameLast)在第一个和最后一个位置,而其他名称应按字母顺序排序.



I have shown the simple example but the actual sorting is quite complex. Hence I have used IComparer .

Now during sorting I want to keep, some names ( e.g.: nameFirst and nameLast) at the first and last location , and other names should get sorted as alphabetically .

How to customize sorting for such a scenario?

推荐答案

在实现Compare的类中,您需要添加两个字段/属性:
In the class that implements Compare you need to add two fields/properties:
private Feature _alwaysFirst;
private Feature _alwaysLast;


并在比较器中执行以下操作:


And do this in your comparer:

public int Comparer(Feature left, Feature right)
    {
    if (left == _alwaysFirst)
        return 1;
    if (left == _alwaysLast)
        return -1;
    return string.Compare(left.FirstName, right.FirstName);
    }


这种体系结构使您只能选择一个功能(员工)首先出现,而最后一个则出现.

请注意,名称不是唯一的,所以我不建议制作_ alwaysFirst和_ alwaysLast字符串,并与left.FirstName进行比较.当然,它将对列表中所有具有相同的名字/姓氏且与_ alwaysXXX名称在开头/结尾处匹配的元素进行分组,但是不会在这些组中进行排序(好吧,您必须这样做)其他一些比较,而不是返回1/-1来在这些组中进行排序.)


This architecture allows you to select just one Feature (employee) to appear first, and just one to appear last.

Note that names are not unique, so I don''t recommend making _alwaysFirst and _alwaysLast strings, and comparing against left.FirstName. Sure, it would group all elements of the list which have identical first/last names that match the _alwaysXXX names at the beginning/end, but it wouldn''t sort within those groups (well, you''d have do some other compare instead of returning 1/-1 to get sorting within those groups).


我只需要在功能"类定义中添加一个字段即可保存实例的得分并将您的代码修改为

I would just add one more field in your "Feature" class definition to hold the score of the instance and modify your code as

public int Compare(firstEmp, secondEmp)
        {
            int diffResult = 0;
diffResult = string.Compare(firstEmp.FirstName, secondEmp.FirstName)*(firstEmp.Score-secondEmp.Score);
            return diffResult;
        }



简而言之,在您的对象上再添加一个评估标准,这样,比起顶部的一个名称和底部的一个名称,比较可以更加动态.您想放在排序顶部的任何名称都会给他们更大的分数,而您想放在底部的名字也一样.并保留默认值0,以免影响排序.

祝你好运



in simple terms, add one more valuation criteria on your objects so that comparing can be more dynamic than just one name on the top and one at the bottom. what ever names you want to put on the top of you sorting give them a bigger score and the same for the ones you want keep to the bottom. and keep the default 0 so that it won''t affect sorting.

good luck


我不知道你为什么不使用LINQ.

您可以轻松使用

collection.OrderBy
这是一个新概念,不需要实现IComparer.
I dont know why dont you employ LINQ into place.

You can easily use

collection.OrderBy
which is new concept and doesnt need to implement IComparer.


这篇关于自定义排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆