954,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C# function for sort

i need a program than can be ask user : name , age , id , and salary
then shows me the maximum salary and minimum salary

and then sort salary from maximum to minimum .

please complete these program for me

public string name;a
        public int age;
        public bool ID;
        private int salary;
        public void Staff()
        {
            Console.WriteLine("enter your name");
            name = Console.ReadLine();
            Console.WriteLine("enter your age : ");
            age = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("enter your ID:");
            ID = Console.ReadLine();
            Console.WriteLine("enter your salary:");
            income = Convert.ToInt32(Console.ReadLine());

}

drfarzad
Light Poster
26 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

You can read either in LINQ or ArrayList, in your case I prefer to read in ArrayList.

Ramy Mahrous
Postaholic
2,196 posts since Aug 2006
Reputation Points: 480
Solved Threads: 276
 

Make a struct from your data :
struct Employee
{
string name;
public int age;
public bool ID;
private int salary;
}
Now make an ArrayList of Employee as RamyMahrous pointed out.

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

can you please complete this issue for me ?

drfarzad
Light Poster
26 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

We will give you bricks, wood, mortar, any other materials if needed.
We will not build your house.

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

Give us hint you tried, if you declare instance from ArrayList class and pressed '.' you'll find someone much greater than us with all respect to my friend Ddanbe. it's intellisense and documentation.

Ramy Mahrous
Postaholic
2,196 posts since Aug 2006
Reputation Points: 480
Solved Threads: 276
 

have a look in the helpfile as to try, and then show us your code, and the errors and/or explain in what way it hadnt done what you wanted..

Then we will help

LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
 

this is my confused program and it dose not as well . it sort some of character from min to max but i want to it sort my salary after the program gives me the salary number

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication54
{
    struct human
    {

        public string name;
        public int age;
        public int ID;
        private int salary;
        public void Staff()
        {
            Console.WriteLine("enter your name");
            name = Console.ReadLine();
            Console.WriteLine("enter your age : ");
            age = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("enter your ID:");
            ID = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("enter your salary:");
            salary = Convert.ToInt32(Console.ReadLine());
        }



        class Program
        {
            static void Main(string[] args)
            {
                string[] cArray = new string[5] { "Cha3123", "Cha1234", "Cha1243", "Cha34", "Cha53" };
                Array.Sort(cArray, StringComparer);
                foreach (string s in cArray)
                {
                    Console.WriteLine(s);
                }
                Console.ReadLine();
            }
            static int StringComparer(object a, object b)
            {
                int stra = Convert.ToInt32((a as string).Remove(0, 3));
                int strb = Convert.ToInt32((b as string).Remove(0, 3));
                return stra - strb;
            }
        }
    }
}
drfarzad
Light Poster
26 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

What's this??
You need to read in Structures as well.
Let us start.
1- I need you do initialize an array of 5 elements from human datatype

Ramy Mahrous
Postaholic
2,196 posts since Aug 2006
Reputation Points: 480
Solved Threads: 276
 

No, it sorted the numbers as you told it to - or at least it did when I ran it.

LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
 
No, it sorted the numbers as you told it to - or at least it did when I ran it.

i want to it show me the max and min salary and the sort number after i gave it to program not the character are in the this line

string[] cArray = new string[3] { "Cha3123", "Cha1234", "Cha1243" };

------------
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication54
{
struct Salary
{

public string name;
public int age;
public int ID;
private int salary;
public void Staff()
{
Console.WriteLine("enter your name");
name = Console.ReadLine();
Console.WriteLine("enter your age : ");
age = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("enter your ID:");
ID = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("enter your salary:");
salary = Convert.ToInt32(Console.ReadLine());
}

class Program
{
static void Main(string[] args)
{
string[] cArray = new string[3] { "Cha3123", "Cha1234", "Cha1243" };
Array.Sort(cArray, StringComparer);
foreach (string s in cArray)
{
Console.WriteLine(s);
}
Console.ReadLine();
}
static int StringComparer(object a, object b)
{
int stra = Convert.ToInt32((a as string).Remove(0, 3));
int strb = Convert.ToInt32((b as string).Remove(0, 3));
return stra - strb;
}
}
}
}

drfarzad
Light Poster
26 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 
namespace ConsoleApplication54


Seems to me you have tried already something out!
But you look like a blind man who is trying to break an egg with a stick!
So first things first : forget for a moment your end result.
Concentrate on making a struct :

struct human
{
    string name;
    int age;
    bool ID;
    int salary;
}

Make a program with only the struct. Does it compile? Does it run?
Find out what a struct is Google, MSDN, books you have, Help menu.
If you done all that try to go a step further, if you are stuck come back.

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

i want to it show me the max and min salary and the sort number after i gave it to program not the character are in the this line

string[] cArray = new string[3] { "Cha3123", "Cha1234", "Cha1243" };

Well your code only dumps out the values in the array, if you wish for min and max then you would have to add the code for that.. But

It printed them out in numerical order for me when I ran your code

Which is exactly what you told it to.
If you wish it to do so *AFTER* you have entered some data, change your code to ask for some data, and store it, and then run the sort.

LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
 

Concentrate on making a struct :

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication54
{
 
        struct salary
        {
        string name;
       int age;
        int ID;
        int salary;
    }
        public void staff ()
            {
            Console.WriteLine("enter your name");
            name = Console.ReadLine();
            Console.WriteLine("enter your age : ");
            age = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("enter your ID:");
            ID = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("enter your salary:");
            salary = Convert.ToInt32(Console.ReadLine());
        }

        class array
        {
            public int useff (string array )
            {
                string[] cArray = new string[3] { "Cha3123", "Cha1234", "Cha1243" };
                Array.Sort(cArray, StringComparer);
                foreach (string s in cArray)
                {
                    Console.WriteLine(s);
                }
                Console.ReadLine();
            }
            static int StringComparer(object a, object b)
            {
                int stra = Convert.ToInt32((a as string).Remove(0, 3));
                int strb = Convert.ToInt32((b as string).Remove(0, 3));
                return stra - strb;
            }

        }
        class Program
        {
            static void Main(string[] args)
            {

                Salary m1 = new Salary();




            }
        }
    }

}
drfarzad
Light Poster
26 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

well, seems you don't need to try we've absolved responsibility regards this, I'll try it after some minutes and come back with C# code.

Ramy Mahrous
Postaholic
2,196 posts since Aug 2006
Reputation Points: 480
Solved Threads: 276
 

Here's some codes...

List<Human> Humans = new List<Human>();

        void Add(Int16 humanID, string humanName, Int16 humanAge, float humanSalary)
        {
            Humans.Add(new Human(humanID, humanName, humanAge, humanSalary));
        }

        float GetMaxSalary()
        {
            float maxSalary = 0;
            foreach (Human aHuman in Humans.ToArray())
            {
                if (aHuman.SALARY > maxSalary)
                    maxSalary = aHuman.SALARY;
            }
            return maxSalary;
        }

        float GetMinSalary()
        {
            float minSalary = GetMaxSalary();
            foreach (Human aHuman in Humans.ToArray())
            {
                if (aHuman.SALARY < minSalary)
                    minSalary = aHuman.SALARY;
            }
            return minSalary;
        }

        float GetAverage()
        {
            float totalSalary = 0;
            foreach (Human aHuman in Humans.ToArray())
            {
                totalSalary += aHuman.SALARY;
            }

            return totalSalary/Humans.Count;
        }
    }

    struct Human
    {
        public Int16 ID;
        public string NAME;
        public Int16 AGE;
        public float SALARY;

        public Human(Int16 humanID, string humanName, Int16 humanAge, float humanSalary)
        {
            ID = humanID;
            NAME = humanName;
            AGE = humanAge;
            SALARY = humanSalary;
        }
    }
Ramy Mahrous
Postaholic
2,196 posts since Aug 2006
Reputation Points: 480
Solved Threads: 276
 

float minSalary = GetMaxSalary();
You could also do float minSalary = Humans.Max();

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

Thank you Danny so much, but he may don't have .NET framework 3.5
I hope he replies he could solve the problem.

Ramy Mahrous
Postaholic
2,196 posts since Aug 2006
Reputation Points: 480
Solved Threads: 276
 

Yes , i don't have 3.5 and it's 2
my visual studio is 2005 .

any way i can't run these codes yet ...

drfarzad
Light Poster
26 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

?! I've did for you almost all functions your program should do. Just try to make use of them and have a deep look inside the code to learn how things done.

Ramy Mahrous
Postaholic
2,196 posts since Aug 2006
Reputation Points: 480
Solved Threads: 276
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You