locked
Windows Store apps - Method for ordering from greatest to least value RRS feed

  • Question

  • I have a class

    public class Balls
    {
    
    public Amount {get;set;}
    
    
    }

    I create a list of that class.

    List<Balls> Myballs = new List<Balls>();
    MyBalls.Add(new Balls{Amount = 10});
    MyBalls.Add(new Balls{Amount = 2});

    I want to create a method that orders the Balls classes in my List from Greatest to Least using the Amount Value. It should return a new List<Balls> .

    for ex:

    Private List<Balls> orderFromGreatToLeast( List<Balls> listToOrder)
    {
    
    
    
    }
    How can i write thos method?

    Tuesday, September 10, 2013 1:14 PM

Answers

  • Try this

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        List<Balls> MyBalls = new List<Balls>();
        MyBalls.Add(new Balls { Amount = 4 });
        MyBalls.Add(new Balls { Amount = 5 });
        MyBalls.Add(new Balls { Amount = 8 });
        MyBalls.Add(new Balls { Amount = 6 });
    
        var Greatest_to_least = orderFromGreatToLeast(MyBalls);
    }
    
    private List<Balls> orderFromGreatToLeast(List<Balls> listToOrder)
    {
        return listToOrder.OrderBy(ball => ball.Amount).Reverse().ToList();
    }
    


    • Marked as answer by kalel111 Sunday, September 15, 2013 1:37 AM
    Wednesday, September 11, 2013 5:49 AM

All replies

  • Anyone know??
    Tuesday, September 10, 2013 1:15 PM
  • public class Balls
        {
            public int Amount { get; set; }
        }
        class BallComparer : IComparer<Balls>
        {
            public int Compare(Balls x, Balls y)
            {
                return x.Amount.CompareTo(y);
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                List<Balls> Myballs = new List<Balls>();
                MyBalls.Add(new Balls { Amount = 10 });
                MyBalls.Add(new Balls { Amount = 2 });
    
                //sort the list in place
                Myballs.Sort(new BallComparer());
    
                //copy to a new list
                List<Balls> newListOfBalls = Myballs.OrderBy(b => b.Amount).ToList();
    
            }
           
        }
    David

    David http://blogs.msdn.com/b/dbrowne/

    • Proposed as answer by Sachin S Tuesday, September 10, 2013 2:11 PM
    • Unproposed as answer by kalel111 Tuesday, September 10, 2013 2:59 PM
    Tuesday, September 10, 2013 1:27 PM
  • Does this work for when there are more than 2 Balls classes in my list?

    So the above method turns a List containing this:

    MyBalls.Add(new Balls { Amount = 4 });
    MyBalls.Add(new Balls { Amount = 5 });

    MyBalls.Add(new Balls { Amount = 8});

    MyBalls.Add(new Balls { Amount = 6 });
     

    Into a list containing this?

    MyBalls.Add(new Balls { Amount = 8 });
    MyBalls.Add(new Balls { Amount = 6 });

    MyBalls.Add(new Balls { Amount = 5});

    MyBalls.Add(new Balls { Amount = 4 });

    • Edited by kalel111 Tuesday, September 10, 2013 2:59 PM
    Tuesday, September 10, 2013 2:48 PM
  • return listToOrder.OrderBy(ball => ball.Amount).ToList();

    is sufficient to order them and if you want Greatest to Least

    return listToOrder.OrderByDescending(ball => ball.Amount).ToList();



    • Proposed as answer by Ben - GGT Tuesday, September 10, 2013 7:38 PM
    • Edited by Ben - GGT Wednesday, September 11, 2013 9:05 AM clarity
    Tuesday, September 10, 2013 7:03 PM
  • Try this

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        List<Balls> MyBalls = new List<Balls>();
        MyBalls.Add(new Balls { Amount = 4 });
        MyBalls.Add(new Balls { Amount = 5 });
        MyBalls.Add(new Balls { Amount = 8 });
        MyBalls.Add(new Balls { Amount = 6 });
    
        var Greatest_to_least = orderFromGreatToLeast(MyBalls);
    }
    
    private List<Balls> orderFromGreatToLeast(List<Balls> listToOrder)
    {
        return listToOrder.OrderBy(ball => ball.Amount).Reverse().ToList();
    }
    


    • Marked as answer by kalel111 Sunday, September 15, 2013 1:37 AM
    Wednesday, September 11, 2013 5:49 AM