hi everyone

i have 1 question regarding array in c# application

how i arrange numbers in ascending or descending order in array using c# software

i do this

int array=new int[1,2,3,4,5]

furhter i cant processed

plz can anyone tell me about this

thankx..

Recommended Answers

All 11 Replies

First, start without syntax errors:

int[] array = new int[] { 3, 2, 1, 5, 4 };

Then you can use something like the LINQ OrderBy or OrderByDescending methods.

First, start without syntax errors:

int[] array = new int[] { 3, 2, 1, 5, 4 };

Then you can use something like the LINQ OrderBy or OrderByDescending methods.

int[] array=new int[] {1,2,3,4,5};
for (int i=0;i>6;i++);
(int j=0;i;j>6-1;j++);


is this correct ??

No.

Arranging them and printing them are two different things.
...but that code will not do what you expect.
You need something more like:

for (int i = 0; i < array.Length; i++)
         {
            Console.Write(array[i]);
         }


         for (int j = array.Length; j > 0; j--)
         {
            Console.Write(array[j-1]);
         }

""""how i arrange numbers in ascending or descending order in array using c# software""""


C# is a programming langauge not a software. If you want to sort the array using C#, you have to do it using loop, I am not giving exact code but the here is the pseudo code:

Loop through all element of Array (outer loop)
Loop through all element of Array (inner loop)
compare outer loop element with inner loop element (greater than or less than according to asc or desc)

swap the element of arrays

end innerloop

end outer loop

No.

Arranging them and printing them are two different things.
...but that code will not do what you expect.
You need something more like:

for (int i = 0; i < array.Length; i++)
         {
            Console.Write(array[i]);
         }


         for (int j = array.Length; j > 0; j--)
         {
            Console.Write(array[j-1]);
         }

thankx ...when i run this it can be on 1 line
when i run program show this

1234554321

i want to show separate like this
5
4
3
2
1

Use Console.WriteLine instead of Console.Write.

Use Console.WriteLine instead of Console.Write.

if i can run same program in windows form application then how we can done

Use a label to show the original array, then use a multi-line text box to show the result when a button is pushed.

Use a label to show the original array, then use a multi-line text box to show the result when a button is pushed.

i use windown form application...and in this i can use tabcontrol
in one tab the text box and save button and other tab there is a button which is RETRIEVE
and listbox ...
when i save numbers the in tab2 i click retrieve button to show numbers which i save it ...
and i want this numbers is decsending order...

i upload the picture but there is a problem in it...here is some coding

this is coding of form

namespace WindowsFormsApplication4
{

    public partial class Form1 : Form
    {
        add myadd = new add();
        public Form1()
        {
            InitializeComponent();
        }


        private void button2_Click(object sender, EventArgs e)
        {
            myadd.data(textBox1.Text);
            label1.Text = myadd.count.ToString();

        }

adn this coding of calss which i made

namespace WindowsFormsApplication4
{
    class add
    {
        string[] mydata = new string[10];
        int mpointer = 0;

        public int count
        {
            get
            {
                return mpointer;
            }
        }
        public void data(string input)
        {
            if (mpointer < mydata.Length)
            {
                mydata[mpointer] = input;
                mpointer++;
            }
            else
            {
                throw new Exception("not more sapce left");
            }
        }
        public string[] getdata()
        {
            return mydata;

then i dont understadn how i arrange these numbers in decending order :( :(

Here is the code to arrange 10 numbers in array in ascending order.
After executing the code enter ten numbers in console with space in each of the numbers

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

            int[] arr = new int [10];
            arr = Console.ReadLine().Split(' ').Select(n => Convert.ToInt32(n)).ToArray(); 

            int temp=0;
            for (int j = 0; j < arr.Length; j++)
            {


                for (int i = 0; i < arr.Length; i++)
                {
                    if (i != (arr.Length - 1))
                    {
                        if (arr[i] > arr[i + 1])
                        {
                            temp = arr[i];
                            arr[i] = arr[i + 1];
                            arr[i + 1] = temp;
                        }
                    }

                }
            }
            for (int i = 0; i < arr.Length; i++)
            {
                Console.Write(arr[i]);
            }
            Console.ReadLine();
        }
    }

For sorting array in ascending order..

Array.Sort(arr)

For sorting Array in descending order..

Array.Sort(array);
Array.Reverse(array);

Also you can use LINQ..

array = array.OrderByDescending(c => c).ToArray();

Full Source...Array Sort

Crony

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.