# How to sort one-dimensional array in ascending order using non-static method?

CsharpProgrammingServer Side Programming

#### Complete Python Prime Pack

9 Courses     2 eBooks

#### Artificial Intelligence & Machine Learning Prime Pack

6 Courses     1 eBooks

#### Java Prime Pack

9 Courses     2 eBooks

Set the unsorted array first.

int[] list = {87, 45, 56, 22, 84, 65};

Now use a nested for loop to sort the list, which is passed to a function.

for(int i=0; i< arr.Length; i++) {
for(int j=i+1; j<arr.Length; j++) {
if(arr[i]>=arr[j]) {
temp=arr[j];
arr[j]=arr[i];
arr[i]=temp;
}
}
Console.Write(arr[i] + " ");
}

The following is the complete code to sort one-dimensional array in ascending order using non-static method.

## Example

Live Demo

using System;
namespace Demo {
public class MyApplication {
public static void Main(string[] args) {
int[] list = {87, 45, 56, 22, 84, 65};
Console.WriteLine("Original Unsorted List");
foreach (int i in list) {
Console.Write(i + " ");
}
MyApplication m = new MyApplication();
m.sortFunc(list);
}
public void sortFunc(int[] arr) {
int temp = 0;
Console.WriteLine("Sorted List");
for(int i=0; i< arr.Length; i++) {
for(int j=i+1; j<arr.Length; j++) {
if(arr[i]>=arr[j]) {
temp=arr[j];
arr[j]=arr[i];
arr[i]=temp;
}
}
Console.Write(arr[i] + " ");
}
}
}
}

## Output

Original Unsorted List
87 45 56 22 84 65
Sorted List
22 45 56 65 84 87
Updated on 23-Jun-2020 14:20:04