Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

array sorting C#

Array.Sort(array); // this will Modify original Array
Comment

array sort C Sharp

var sortedArray = arr.OrderBy(x => x); // OrderByDescending()
Comment

c# sort int array

 /// <summary>
        /// Sort Int Array
        /// </summary>
        /// <param name="sortBy">A- Ascendig, D- Descending order</param>
        /// <param name="arr">int array to sort</param>
        /// <returns>Sorted Array </returns>
/// Sorting without using the build sort in C# and using unlimited int array parameters using the params keyword
        public static int [] SortArray(char sortBy, params int [] arr)
        {
            int tmp;
            for(int x =0; x< arr.Length - 1; x++)
            {
                // traverse i + 1 
                for(int j =x + 1; j < arr.Length; j++)
                {
                    //compare and swap
                    if (sortBy.Equals('A'))
                    {
                        if (arr[x] > arr[j])
                        {
                            tmp = arr[x];
                            arr[x] = arr[j];
                            arr[j] = tmp;
                        }
                    }else if(sortBy.Equals('D'))
                    {
                        if (arr[x] < arr[j])
                        {
                            tmp = arr[x];
                            arr[x] = arr[j];
                            arr[j] = tmp;
                        }
                    }else
                    {
                        if (arr[x] > arr[j])
                        {
                            tmp = arr[x];
                            arr[x] = arr[j];
                            arr[j] = tmp;
                        }
                    }
                }
            }
            return arr;
        }
Comment

c# sort array

 int[] sortedCopy = from element in copyArray 
                    orderby element ascending select element;
Comment

c# sort array by value

// See https://aka.ms/new-console-template for more information
string[] myStrArr = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"};
int[] myIntArr = {5, 6, 4, 7, 85, 9, 2, 12, 3, 1};

// or use Array.Reverse() for DESC
Array.Sort(myStrArr);
Array.Sort(myIntArr);
// Array.Sort() for ASC

myStrArr.ToList().ForEach(i => Console.WriteLine(i.ToString()));
myIntArr.ToList().ForEach(i => Console.WriteLine(i));
Comment

PREVIOUS NEXT
Code Example
Csharp :: custom click event wpf button 
Csharp :: what is unity 
Csharp :: char array 
Csharp :: c# backup sql 
Csharp :: type or namespace text could not be found unity 
Csharp :: deserialize json to dynamic object c# 
Csharp :: TimeZone in asp.net core 
Csharp :: c# function 
Csharp :: get the number of cpu c# 
Csharp :: c# get all classes derived from type 
Csharp :: how to create function in c# 
Csharp :: random string generator c# 
Csharp :: defualtsize UWP c# 
Csharp :: c# substring until character single 
Csharp :: if list does not contain then add c# 
Csharp :: vb.net get date minus one day 
Csharp :: c# get distinct values all fields from list 
Csharp :: c# comment 
Csharp :: sum of digit of number c# 
Csharp :: c# string 
Csharp :: how to get mouse position c# 
Csharp :: C# loop through the registry searching for keys containing 
Csharp :: freeze scene unity 
Csharp :: httpclient 
Csharp :: properties in c# 
Csharp :: linq map array 
Csharp :: c# Intersectcase insensitive 
Csharp :: Screen.lockcursor unity 
Csharp :: how to make randomizer c# 
Csharp :: blazor clientside cookies 
ADD CONTENT
Topic
Content
Source link
Name
1+4 =