当前位置:  开发笔记 > 编程语言 > 正文

我如何使用IComparable接口?

如何解决《我如何使用IComparable接口?》经验,为你挑选了2个好方法。

我需要一个如何使用IComparable接口的基本示例,以便我可以按升序或降序排序,并按我正在排序的对象类型的不同字段排序.



1> Marc Gravell..:

好吧,因为你使用List它会比使用a简单得多Comparison,例如:

List data = ...
// sort by name descending
data.Sort((x,y) => -x.Name.CompareTo(y.Name));

当然,使用LINQ你可以使用:

var ordered = data.OrderByDescending(x=>x.Name);

但是你可以List很容易地重新引入它(用于就地重新排序); 下面是允许例如SortList与lambda语法:

using System;
using System.Collections.Generic;  

class Foo { // formatted for vertical space
    public string Bar{get;set;}
}
static class Program {
    static void Main() {
        List data = new List {
            new Foo {Bar = "abc"}, new Foo {Bar = "jkl"},
            new Foo {Bar = "def"}, new Foo {Bar = "ghi"}
        };
        data.SortDescending(x => x.Bar);
        foreach (var row in data) {
            Console.WriteLine(row.Bar);
        }
    }

    static void Sort(this List source,
            Func selector) {
        var comparer = Comparer.Default;
        source.Sort((x,y)=>comparer.Compare(selector(x),selector(y)));
    }
    static void SortDescending(this List source,
            Func selector) {
        var comparer = Comparer.Default;
        source.Sort((x,y)=>comparer.Compare(selector(y),selector(x)));
    }
}



2> Spencer Rupo..:

这是一个简单的例子:

public class SortableItem : IComparable
{
    public int someNumber;

    #region IComparable Members

    public int CompareTo(SortableItem other)
    {
        int ret = -1;
        if (someNumber < other.someNumber)
            ret = -1;
        else if (someNumber > other.someNumber)
            ret = 1;
        else if (someNumber == other.someNumber)
            ret = 0;
        return ret;
    }

    #endregion
}

"那很好,但是如果我想能够控制排序顺序,或者按其他字段排序呢?"

简单.我们需要做的就是为对象添加更多字段.首先,我们将为不同的排序类型添加一个字符串,然后我们将添加一个布尔值来表示我们是按降序还是按升序排序,然后添加一个字段来确定我们要搜索哪个字段.

public class SortableItem : IComparable
{
    public enum SortFieldType { SortNumber, SortString }

    public int someNumber = -1;
    public string someString = "";
    public bool descending = true;    
    public SortFieldType sortField = SortableItem.SortFieldType.SortNumber;        

    #region IComparable Members

    public int CompareTo(SortableItem other)
    {
        int ret = -1;
        if(sortField == SortableItem.SortFieldType.SortString)
        {
            // A lot of other objects implement IComparable as well.
            // Take advantage of this.
            ret = someString.CompareTo(other.someString);
        }
        else
        {
            if (someNumber < other.someNumber)
                ret = -1;
            else if (someNumber > other.someNumber)
                ret = 1;
            else if (someNumber == other.someNumber)
                ret = 0;
        }
        // A quick way to switch sort order:
        // -1 becomes 1, 1 becomes -1, 0 stays the same.
        if(!descending) ret = ret * -1; 

        return ret;
    }

    #endregion

    public override string ToString()
    {
       if(sortField == SortableItem.SortFieldType.SortString)
          return someString;
       else
          return someNumber.ToString();
    }
}

"告诉我怎么样!"

好吧,因为你问得这么好.

static class Program
{
    static void Main()
    {

        List items = new List();
        SortableItem temp = new SortableItem();
        temp.someString = "Hello";
        temp.someNumber = 1;
        items.Add(temp);
        temp = new SortableItem();
        temp.someString = "World";
        temp.someNumber = 2;
        items.Add(temp);
        SortByString(items);
        Output(items);
        SortAscending(items);
        Output(items);
        SortByNumber(items);
        Output(items);
        SortDescending(items);
        Output(items);
        Console.ReadKey();
    }

    public static void SortDescending(List items)
    {
        foreach (SortableItem item in items)
            item.descending = true;
    }
    public static void SortAscending(List items)
    {
        foreach (SortableItem item in items)
            item.descending = false;
    }
    public static void SortByNumber(List items)
    {
        foreach (SortableItem item in items)
            item.sortField = SortableItem.SortFieldType.SortNumber;
    }
    public static void SortByString(List items)
    {
        foreach (SortableItem item in items)
            item.sortField = SortableItem.SortFieldType.SortString;
    }
    public static void Output(List items)
    {
        items.Sort();
        for (int i = 0; i < items.Count; i++)
            Console.WriteLine("Item " + i + ": " + items[i].ToString());
    }
}


如果你有更好的想法,没有任何东西说你也无法回答这个问题.
推荐阅读
手机用户2502852037
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有