- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to sort one dimensional array in descending order using non static method?
Set the unsorted list first.
int[] list = {87, 45, 56, 22, 84, 65};
Now use a nested for loop to sort the list that is passed to a function.
for(int i=0; ilt; 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 descending order.
Example
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 87 84 65 56 45 22
- Related Articles
- How to sort one-dimensional array in ascending order using non-static method?
- How to sort one dimensional array in descending order using Array Class method?
- Sort an array in descending order using C#
- C# program to sort an array in descending order
- C program to sort an array in descending order
- How to sort List in descending order using Comparator in Java
- How to print one dimensional array in reverse order?
- 8086 program to sort an integer array in descending order
- How do you sort an array in C# in descending order?
- How to perform descending order sort in MongoDB?
- How to sort a Vector in descending order using STL in C++?
- How to sort TreeSet in descending order in Java?
- Python program to sort the elements of an array in descending order
- How to sort an ArrayList in Descending Order in Java
- How to sort an ArrayList in Java in descending order?

Advertisements