Jump to content

list sorting

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
Siten0308

Siten0308

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 302 posts
Hello,

using the same example programming that I made earlier, I know its probably not a good example because it will already be sorted by the list index (or value), but if you can give me a simple example of how to implement sorting using a list and dictionary if anyone can, I know its something like:


list<string> names = list<string>();

names.add("blah");

names.add(meh");

names.add(huh");

foreach(names naming in names)

{

names.Sort();

Console.WriteLine(naming);

}


can anyone correct this to the proper way to do the sort or where to put the sort? also can anyone let me know how to use the sort in dictionary, when looking online they said to use a list for the dictionary keys then use sort within the list, is that correct?

thanks

#2
ArekBulski

ArekBulski

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,376 posts
There are "simple lists" like with string or int elements, and there are "other lists" which have classes or structs as elements.

If you have a simple list, like List<string> then I advise you to just use the most simple Sort() method there is. It uses a default comparer. Here is my own code: ;)


    List<string> stringList = new List<string>();

    stringList.Add("first");

    stringList.Add("second");

    stringList.Add("001234");

    stringList.Add("aabbcc");


    /// uses the default comparer!

    stringList.Sort();


    /// then use foreach to iterate through sorted list

    /// check attached solution, and play with it


I found a great article about easy, yet powerful sorting of Lists of classes. Please take a closer look. C# Lists: Add some elegance to your code :cool:

If you have a List of Classes, then I think you need to do some "custom sorting", which I will copy from the article above. This approach is very clean, no spaghetti, so I would use it myself. :)


List<person>people = new List<person>();

people.Add(new Person(50, "Fred"));

people.Add(new Person(30, "John"));


people.Sort(delegate(Person p1, Person p2) 

   { return p1.name.CompareTo(p2.name); });


I will think about sorting Dictionaries later on.

Best regards to Siten, :cool:
Arek Bulski.

Attached Files



#3
Siten0308

Siten0308

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 302 posts
cool thanks again ArekBulski,

Last question:

Here is the code below,

The questions are, am I just removing the variables from whatever the user specifies example (if user wants to remove user 1, they type in user 1 and it removes all of user 1 info) which does that happen in this code, or am I just removing whats in the list and not the key? I am also getting an error message saying "No overload for method 'removeitem' takes '1' argument", I know i did it right because the removeitem (which is found in the employee class) has 3 in its parameters which contains 2 strings and 1 int, shouldnt it not get that error message anyways, but most importantly, am I using the removing method correctly to remove the user information and the key variable so the other variables can move up?

code:
main code:

class Program

    {

        static void Main(string[] args)

        {

            string name2,name3;

            int age4;

            employee employeeremoval = new employee(name2, name3, age4);

            List<employee> employeedisplay = new List<employee>();

            string answer;

            

            Console.WriteLine("would you like to enter employee information?");

            answer = Console.ReadLine();

            while (answer.ToLower() != "yes" && answer.ToLower() != "no")

            {

                Console.WriteLine("Please answer yes or no");

                answer = Console.ReadLine();

            }

            while (answer.ToLower() == "yes")

            {

                string name1, lastname1, answer1;

                int age, numanswer1;

                

                Console.WriteLine("Please type in first name:");

                name1 = Console.ReadLine();

                Console.WriteLine("Please type in last name:");

                lastname1 = Console.ReadLine();

                Console.WriteLine("Type in Age:");

                age = Convert.ToInt32(Console.ReadLine());

                employee employeeform = new employee(name1, lastname1, age);

                Console.WriteLine("would you like to enter employee information?");

                answer = Console.ReadLine();

                employeedisplay.Add(new employee(name1, lastname1, age));

                Console.WriteLine("Here are the added employess");

                foreach (employee employeethis in employeedisplay)

                {

                    Console.WriteLine(employeethis.shownames());

                }

                Console.WriteLine("Who would you like to remove?");

                answer1 = Console.ReadLine();

                if (answer1.ToLower() != "yes" && answer1.ToLower() != "no")

                {

                    Console.WriteLine("Good bye");

                }

                if (answer.ToLower() == "yes")

                {

                    foreach (employee employment in employeedisplay)

                    {

                        Console.WriteLine(employment.shownames());

                    }

                    Console.WriteLine("Who would you like to remove? type number if of the person you want to remove");

                    numanswer1 = employeeremoval.removeitem(new employee(name1, lastname1, age));


                    while (answer.ToLower() != "yes" && answer.ToLower() != "no")

                    {

                        Console.WriteLine("Please answer yes or no");

                        answer = Console.ReadLine();

                    }

                }

                if (answer.ToLower() == "no")

                {

                    Console.WriteLine("Good Bye :) ");

                    System.Threading.Thread.Sleep(3000);

                }


            }


class employee

class employee

    {

        private string firstname, lastname;

        private int age;


        public employee()

        {

        }

        

        public employee(string firstname, string lastname, int age)

        {

            this.Firstname = firstname;

            this.Lastname = lastname;

            this.Age = age;

        }


        public string Firstname

        {

            get { return firstname; }

            set { firstname = value; }

        }

        public string Lastname

        {

            get { return lastname; }

            set { lastname = value; }

        }

        public int Age

        {

            get { return age; }

            set { age = value; }

        }


        public string shownames()

        {

            return String.Format("Employee name: " + Firstname + "\nEmployee lastname: " + Lastname + "\nAge: " + Age);

        }


        public void removeitem(string firstname, string lastname, int age)

        {

            List<employee> removing = new List<employee>();

            removing.Remove(new employee(firstname, lastname, age));

        }

    }


again sorry for all the spegehtti code, just wanted to cook something up real fast, and typing a whole bunch of methods would seem to long for me, imo, and sorry for not using PASCAL naming convention.

But any input on this what am I doing wrong and how to implement .remove to employeeremoval would be great : )

Thanks in advance