How to sort() a custom class?

techbro
techbro used Ask the Experts™
on
I am studying for MCTS exam, and figuring out how to sort custom class by sort() method.
I have created a "Burger" class, implemented IComparable, accessor variable, and inserted some integers inside 6 instances of class. But I am not sure how to sort them by sort() method so that I could print the integers in asecending order by Console.WriteLine().
Can you provide me inputs how to use sort() in such circumstance?

The code is given below:

namespace CustomClassSort
{

    class Burger : IComparable<Burger>
    {
        private int burger;
        public Burger(int burger)
        {
            this.burger = burger;
        }

        public int CompareTo(Burger other)
        {
            return this.iloveburger.CompareTo(other.iloveburger);            
        }

        public int iloveburger
        {
            get
            {
                return this.burger;
            }
            set
            {
                this.burger = value;
            }
        }
       
    }
    
    class IDateABurger
    {
        static void Main(string[] args)
        {
            Burger a = new Burger(2);
            Burger b = new Burger(22);
            Burger c = new Burger(12);
            Burger d = new Burger(33);
            Burger e = new Burger(44);
            Burger f = new Burger(5);

        }
    }
} 

Open in new window


Let me know if you have any question.

P.S: I don't actually date a burger. :)
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
Look at the overloads for Sort and see if you can tell how you might use that to sort  ; )
Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Commented:
Actually, strike that. You're not using the class I thought you were.

Think about what the return value of CompareTo signifies. There are only 3 possible values that method returns (unless some yahoo decides to return a different value, which doesn't really matter anyway). Once you understand those return values, you should be able to see how to manipulate your CompareTo method to return an ascending or descending sort.
HainKurtSr. System Analyst

Commented:
try:

public int compareTo(Burger other) {
        If (this.Burger < other.Burger) return -1;
        If (this.Burger == other.Burger) return 0;
        If (this.Burger > other.Burger) return +1;
}
Commented:
Or try this:
Burger[] arr = new Burger[]{a,b,c,d,e,f};
Array.Sort(arr);
foreach(var item in arr)
Console.WriteLine(item.iloveburger);

Open in new window

Commented:
Or using lambda:
Burger[] arr = new Burger[]{a,b,c,d,e,f};
foreach(var item in arr.OrderBy(m=>m.iloveburger))
Console.WriteLine(item.iloveburger);
HainKurtSr. System Analyst
Commented:
oops, burger property is private, try this:

public int compareTo(Burger other) {
        If (this.iloveburger < other.iloveburger) return -1;
        If (this.iloveburger == other.iloveburger) return 0;
        If (this.iloveburger > other.iloveburger) return +1;
}
 
kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
@morgulo
That's not going to help him for the MCTS exam  ; )
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
You could use a List<> instead:
static void Main(string[] args)
        {
            List<Burger> Burgers = new List<Burger>();
            Burgers.Add(new Burger(2));
            Burgers.Add(new Burger(22));
            Burgers.Add(new Burger(12));
            Burgers.Add(new Burger(33));
            Burgers.Add(new Burger(44));
            Burgers.Add(new Burger(5));
            Burgers.Sort();

            foreach (Burger burger in Burgers)
            {
                Console.WriteLine(burger.iloveburger.ToString());
            }

            Console.Write("Press Enter to Quit...");
            Console.ReadLine();
        }

Open in new window

Author

Commented:
Thanks for input.
I will add another post in the "Related Question" option, if I have more questions.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial