Array.Sort(array); // this will Modify original Array
var numbers = [4, 2, 5, 1, 3];
numbers.sort(function(a, b) {
return a - b;
});
console.log(numbers);
// [1, 2, 3, 4, 5]
a = ['12', '24', '44', '32', '55']
a.sort! # a is now sorted
a.sort # returns a sorted array, but does not modify a itself. Typical use: b = a.sort
let sortArray = array.sorted(by: { $0.name.lowercased() < $1.name.lowercased() })
go mod edit -go=1.18
int current = 0;
for (int i = 0; i < array.length ; i++) {
for (int j = i+1; j < array.length ; j++) {
if (array[i]>array[j]) {
current = array[i];
array[i] = array[j];
array[j] = current;
}
}
}
//The sort() method sorts an array alphabetically:
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();
//output >> [ "Apple",Banana","Mango", "Orange" ]
//if you find the answer is useful ,
//upvote ⇑⇑ , so can the others benefit also . @mohammad alshraideh ( ͡~ ͜ʖ ͡°)
// C++ code for k largest elements in an array
#include <bits/stdc++.h>
using namespace std;
void kLargest(int arr[], int n, int k)
{
// Sort the given array arr in reverse
// order.
sort(arr, arr + n, greater<int>());
// Print the first kth largest elements
for (int i = 0; i < k; i++)
cout << arr[i] << " ";
}
// driver program
int main()
{
int arr[] = { 1, 23, 12, 9, 30, 2, 50 };
int n = sizeof(arr) / sizeof(arr[0]);
int k = 3;
kLargest(arr, n, k);
}
// This article is contributed by Chhavi