Sort 3 lists according to their values

I have 3 lists that contain the number of doubling pairs. Now I want to compare these lists with each other and sort them. The entire list is the same length.

Sort Ratio:

Compare each item. A list that has more items that are larger than others is ordered. I wrote an implementation for two lists:

public static bool isGreater(List<double> first, List<double> second)
        {
            int firstCounter = 0;
            int secondCounter = 0;

            for (int i = 0; i < first.Count; i++)
            {
                if (first.ElementAt(i) > second.ElementAt(i))
                {
                    firstCounter++;
                }
                else if (first.ElementAt(i) < second.ElementAt(i))
                {
                    secondCounter++;
                }
            }

            if (firstCounter > secondCounter)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

But how can I adapt this code for 3 or even n lists?

+3
source share
9 answers

You must do this using LINQ and custom icomparer for IEnumerable<double>.

public class EnumerableDoubleComparer : IComparer<IEnumerable<double>>
{
    public int Compare( IEnumerable<double> a, IEnumerable<double> b )
    {
        var counts = a.Select( (k,i) => new { Value = k, Index = i } )
                      .Join( b.Select( (k,i) => new { Value = k, Index = i } ),
                             outer => outer.Index,
                             inner => inner.Index,
                             (outer,inner) => outer.Value > inner.Value
                                                  ? "A"
                                                  : (inner.Value > outer.Value
                                                        ? "B"
                                                        : "" ) )
                      .GroupBy( listID => listID )
                      .Select( g => new { g.Key, Count = g.Count() } );

        // you could also use SingleOrDefault on the collection and check for null
        var aCount = counts.Where( c => c.Key == "A" )
                           .Select( c => c.Count )
                           .SingleOrDefault();
        var bCount = counts.Where( c => c.Key == "B" )
                           .Select( c => c.Count )
                           .SingleOrDefault();

        return aCount - bCount;
    }
}

Used as:

var a = new double[] { 1, 1 };
var b = new double[] { 2, 2 };
var c = new double[] { 3, 3 };

var lists = new List<IEnumerable<double>> { a, c, b };

var ordered = lists.OrderByDescending( l => l, new EnumerableDoubleComparer() );
+2
source

sort() . .

:

  • , .
  • sort() , .

EDIT: max O (M N lgN) (N M). ( ) , max. . , .

+2

?

.

, n-, , . , , .

+1

.

n . - Counter

 void SortLists(List<List<double>> lists)
 {
 int[] counter = new int[lists.Count];

 for (int i = 0; i < lists[0].Count; i++)
 {
     double MaxValue = double.MinValue;
     int winningList = 0;

     for (int j = 0; j < lists.Count; j++)
     {
        if(lists[j].ElementAt(i) > MaxValue )
        {
            MaxValue = lists[j].ElementAt(i);
            winningList = j;
        }
     }

     counter[winningList]++;
 }

 // sort the counter array, in effect your lists are sorted.
 }
+1
var list11 = new List<double> { 1, 2, 3 };
var list22 = new List<double> { 4, 5, 3 };
var list33 = new List<double> { 4, 1, 0 };

int acounter = 0, bcounter = 0, ccounter;
list11.Select((o, i) => new { a = list11[i], b = list22[i] })
    .Aggregate(0, (init, item) => item.a > item.b
                                        ? acounter++
                                        : item.a == item.b
                                            ? bcounter
                                            : bcounter++);
if (acounter > bcounter)
{
    //Do the same for list11 and list33
}
else
{
    //Do the same for list22 and list33 
}

you can refactor it and write a function for two list and call that for every pair that you want.

+1

:

private static List<double> GetGreater(List<double> first, List<double> second)
{
    int firstCounter = 0;
    int secondCounter = 0;

    for (int i = 0; i < first.Count; i++)
    {
        if (first.ElementAt(i) > second.ElementAt(i))
        {
            firstCounter++;
        }
        else if (first.ElementAt(i) < second.ElementAt(i))
        {
            secondCounter++;
        }
    }

    // return the greater list instead of bool
    return (firstCounter > secondCounter ? first : second);
}


public static List<double> Greater(params List<double>[] p)
{
    // assumes the first list is the greater, then check the others
    // this method assumes you will always pass at least 2 lists

    List<double> greater = p[0];

    for (var i = 1; i < p.Length; ++i)
    {
        greater = GetGreater(greater, p[i]);
    }

    return greater;
}

static void Main(string[] args)
{
    // lists used for this test
    var l1 = new List<double>() { 1, 222, 3 };
    var l2 = new List<double>() { 1, 2, 4 };
    var l3 = new List<double>() { 11, 222, 333 };

    var l4 = Greater(l1, l2, l3); // l3
}
+1

, "" "" , "" . :

// Set up some input data
var outerList = new List<List<double>>();
outerList.Add(new List<double> { 2.0, 3.0, 3.0 });
outerList.Add(new List<double> { 1.0, 2.0, 3.0 });
outerList.Add(new List<double> { 2.0, 2.0, 3.0 });

// Sort the outer list
outerList.Sort((first, second) => isGreater(first, second) ? 1 : -1);

, , isGreater ( " ", Sort) CompareLists, -1, , 1, , 0, . :

outerList.Sort(CompareLists);
+1

, , :

const int listSize = 6;

List<int> one = new List<int> { 0, 1, 2, 3, 4, 5 };
List<int> two = new List<int> { 10, 1, 9, 2, 8, 3 };
List<int> three = new List<int> { 5, 5, 5, 5, 5, 5 };

Dictionary<List<int>, int> lists = new Dictionary<List<int>, int>()
{
    {one, 0},
    {two, 0},
    {three, 0}
};

for (int i = 0; i < listSize; i++)
{
    var sortedAtIndex = lists.Keys.OrderByDescending(k => k[i]);
    lists[sortedAtIndex.ElementAt(0)]++;
}

// And to show you that it works...
foreach (var element in lists.OrderByDescending(k => k.Value)
    .Select(k => k.Key))
{
    Console.WriteLine("{0}", lists[element]);
}
+1

- - , , , .

, , .

0

Source: https://habr.com/ru/post/1783912/


All Articles