Custom Lambda sort extension

后端 未结 2 1446
眼角桃花
眼角桃花 2021-01-17 06:25

i want to use my custom extension method to order a list of objects. it\'s just a sample so it uses a bubblesort. my current state:

public static IOrderedQue         


        
2条回答
  •  情歌与酒
    2021-01-17 06:34

    You are calling comparer.Compare(Result[j - 1], Result[j]) now. That is wrong, since it doesn't take the key into account as you said.

    You should get the key by calling the Func keySelector, and match that one:

    IComparer comparer = Comparer.Default;
    
    var x = keySelector(Result[j - 1]);
    var y = keySelector(Result[j]);
    
    if (comparer.Compare(x, y) > 0)
    {
        ...
    }
    

提交回复
热议问题