C# 中的排序列表替代方案

发布于 2024-07-25 23:32:25 字数 385 浏览 4 评论 0原文

我有许多对象,全部来自同一类(ColorNum) 每个对象有 2 个成员变量(m_Color 和 m_Number)

示例:

ColorNum1(Red,25)
ColorNum2(Blue,5)
ColorNum3(Red,11)
ColorNum4(White,25)

这 4 个对象位于 ColorNumList

List<ColorNum> ColorNumList = new List<ColorNum>();

现在我想对列表进行排序,以便 mColor =“Red”的对象“位于顶部。 我不关心其余对象的顺序。

我的谓词方法应该是什么样子?

I have a number of objects, all from the same class(ColorNum) Each object has 2 member variabels (m_Color and m_Number)

Example:

ColorNum1(Red,25)
ColorNum2(Blue,5)
ColorNum3(Red,11)
ColorNum4(White,25)

The 4 objects are in the ColorNumList

List<ColorNum> ColorNumList = new List<ColorNum>();

Now I want to order the list so the objects with mColor = "Red" is in the top.
I dont care about the order of the remaining objects.

What should my predicate method look like?

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

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

发布评论

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

评论(3

云醉月微眠 2024-08-01 23:32:26

使用 linq:

var sortedRedAtTop = 
    from col in ColorNumList 
    order by col.Color == Red ? 1 : 2
    select col;

或列表的排序方法:

ColorNumList.Sort( (x,y) => 
    (x.Color == Red ? 1 : 2)-(y.Color == Red ? 1 : 2) );

Using linq:

var sortedRedAtTop = 
    from col in ColorNumList 
    order by col.Color == Red ? 1 : 2
    select col;

Or the list's sort method:

ColorNumList.Sort( (x,y) => 
    (x.Color == Red ? 1 : 2)-(y.Color == Red ? 1 : 2) );
内心荒芜 2024-08-01 23:32:26
ColorNumList.Sort((x, y) => x.m_Color == Red ? 1 : (y.m_Color == Red ? -1 : 0));
ColorNumList.Sort((x, y) => x.m_Color == Red ? 1 : (y.m_Color == Red ? -1 : 0));
尐偏执 2024-08-01 23:32:26

请参阅 http://msdn.microsoft。 com/en-us/library/system.array.sort(VS.71).aspx

[C#] public static void Sort(Array, Array, int, int, IComparer);

您需要实现一个函数来比较两个对象并返回一个值,该值指示一个对象是否小于、等于或大于另一个对象。

http://msdn.microsoft .com/en-us/library/system.collections.icomparer.compare(VS.71).aspx

您需要编写一个实现 IComparer 接口的类。

我没有使用过 C#,但这里是 VB 等效项:

Class ColorCompare
实现 IComparer

    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
        Dim xc As ColorNum = TryCast(x, ColorNum)
        Dim yc As ColorNum = TryCast(y, ColorNum)
        If x.color = Red Then
            Return 1
        ElseIf y.color = Red Then
            Return -1
        Else
            Return 0
        End If
    End Function
End Class

See http://msdn.microsoft.com/en-us/library/system.array.sort(VS.71).aspx

[C#] public static void Sort(Array, Array, int, int, IComparer);

You need to implement a function that compares two objects and returns a value indicating whether one is less than, equal to or greater than the other.

http://msdn.microsoft.com/en-us/library/system.collections.icomparer.compare(VS.71).aspx

You need to write a class that implemenets the IComparer interface.

I haven't been using C#, but here is the VB equivalent:

Class ColorCompare
Implements IComparer

    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
        Dim xc As ColorNum = TryCast(x, ColorNum)
        Dim yc As ColorNum = TryCast(y, ColorNum)
        If x.color = Red Then
            Return 1
        ElseIf y.color = Red Then
            Return -1
        Else
            Return 0
        End If
    End Function
End Class
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文