1,105,578 Community Members

arrange numbers in ascending or descending order using array

Member Avatar
diya45
Junior Poster in Training
66 posts since Dec 2011
Reputation Points: -2 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
thines01
Postaholic
2,420 posts since Oct 2009
Reputation Points: 389 [?]
Q&As Helped to Solve: 413 [?]
Skill Endorsements: 10 [?]
Team Colleague
Featured
 
1
 

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.

Member Avatar
diya45
Junior Poster in Training
66 posts since Dec 2011
Reputation Points: -2 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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.

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

Member Avatar
thines01
Postaholic
2,420 posts since Oct 2009
Reputation Points: 389 [?]
Q&As Helped to Solve: 413 [?]
Skill Endorsements: 10 [?]
Team Colleague
Featured
 
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]);
         }
Member Avatar
sufyan2011
Junior Poster
165 posts since Dec 2011
Reputation Points: -1 [?]
Q&As Helped to Solve: 23 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
diya45
Junior Poster in Training
66 posts since Dec 2011
Reputation Points: -2 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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]);
         }

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

Member Avatar
thines01
Postaholic
2,420 posts since Oct 2009
Reputation Points: 389 [?]
Q&As Helped to Solve: 413 [?]
Skill Endorsements: 10 [?]
Team Colleague
Featured
 
0
 

Use Console.WriteLine instead of Console.Write.

Member Avatar
diya45
Junior Poster in Training
66 posts since Dec 2011
Reputation Points: -2 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Use Console.WriteLine instead of Console.Write.

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

Member Avatar
thines01
Postaholic
2,420 posts since Oct 2009
Reputation Points: 389 [?]
Q&As Helped to Solve: 413 [?]
Skill Endorsements: 10 [?]
Team Colleague
Featured
 
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.

Member Avatar
diya45
Junior Poster in Training
66 posts since Dec 2011
Reputation Points: -2 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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.

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 :( :(

Lishan
Newbie Poster
1 post since Oct 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
Unverified Member
 
0
 

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();
        }
    }
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: