Sort one List<> based on another

前端 未结 4 1138
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-06 23:22

Say I have

List ages  = new List() { 8, 5, 3, 9, 2, 1, 7 };
List marks = new List() { 12, 17, 08, 15, 19, 02, 11          


        
相关标签:
4条回答
  • 2020-12-06 23:53

    Use:

    public static void Sort<TR, TD>(IList<TR> refList, IList<TD> dataList)
            where TR : System.IComparable<TR>
            where TD : System.IComparable<TD>
    {
     ...
    }
    

    and then use:

    refList[i].CompareTo(refList[i+1])
    

    instead of the operators.

    .Net numbers already implement IComparable, and you can use overloads that allow you to specify a different IComparable.

    0 讨论(0)
  • 2020-12-07 00:02

    To sort one list the way you want you actually need to somehow keep references from items in first list to they weight/keys in the second list. No existing methods do that as you can't easily associate metadata with arbitrary values (i.e. if first list is list of int as in your case there is nothing to map to keys in second list). Your only reasonable option is to sort 2 lists at the same time and make association by index - again no existing classes to help.

    It may be much easier to use solution that you reject. I.e. simply Zip and OrderBy, than recreate first list:

    ages = ages
      .Zip(marks, (a,m)=> new {age = a; mark = m;})
      .OrderBy(v => v.mark)
      .Select(v=>v.age)
      .ToList();
    

    Note (courtesy of phoog): if you need to do this type of sorting with Array there is Array.Sort that allows exactly this operatiion (see phoog's answer for details).

    0 讨论(0)
  • 2020-12-07 00:03

    There's no framework method to do this with List<T>, but if you don't mind putting the data into two arrays, you can use one of the Array.Sort() overloads that takes two arrays as arguments. The first array is the keys, and the second is the values, so your code might look like this (leaving aside the step of getting arrays from the lists):

    Array.Sort(ages, marks);
    

    The specifics of getting the values into arrays and then back into lists would depend, among other things, on whether you need to end up with the same list sorted appropriately or whether it's okay to return a new list with the data in the desired order.

    0 讨论(0)
  • 2020-12-07 00:11

    If I understand "I can sort my marks by ages like this:" properly,

    I would like to suggest the below to eliminate much confusion.

    struct Student{
        int age;
        int marks;
    };
    
    List<Student> students = {{8,12}, ...};
    

    Now you can sort according to age and marks is accordingly sorted automatically.

    If it is not possible, you need to fix the code as below.

    First of all, T is almost certainly not the same type in RefList and DataList.

    Then you need 2 parameters T1, T2. Just T implies the types are the same.

    public static void Sort<RefType, DataType>(List<RefType> RefList, List<DataType> DataList)
    {
    

    You can also zip the two lists together as suggested by Mechanical Snail and explained in Looping through 2 Lists at once

    0 讨论(0)
提交回复
热议问题