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

arrange numbers in ascending or descending order using array

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..

diya45
Newbie Poster
5 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

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.

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

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 ??

diya45
Newbie Poster
5 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

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]);
         }
thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

""""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

sufyan2011
Junior Poster
166 posts since Dec 2011
Reputation Points: 9
Solved Threads: 20
 

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

diya45
Newbie Poster
5 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

Use Console.WriteLine instead of Console.Write.

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 
Use Console.WriteLine instead of Console.Write.


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

diya45
Newbie Poster
5 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

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

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 
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 :( :(

diya45
Newbie Poster
5 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: