//Higher order function for sorting
numArray.sort((a, b) => a - b); // For ascending sort
numArray.sort((a, b) => b - a); // For descending sort
const points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){
return a - b
});
////HOF/////
numArray.sort(function(a, b){
return a - b
});
//output >> [1 ,5 , 10 ,25 ,40 ,100]
//sort an array of strings
var fruitSalad = ['cherries', 'apples', 'bananas'];
fruit.sort(); // ['apples', 'bananas', 'cherries']
//sort an array of numbers
var scores = [1, 10, 2, 21];
scores.sort(); // [1, 10, 2, 21]
// Watch out that 10 comes before 2,
// because '10' comes before '2' in Unicode code point order.
//sorts an array of words
var randomThings = ['word', 'Word', '1 Word', '2 Words'];
randomThings.sort(); // ['1 Word', '2 Words', 'Word', 'word']
// In Unicode, numbers come before upper case letters,
// which come before lower case letters.
function eq(x, y) {
if (x < y) return -1;
else if (x > y) return 1;
else return 0;
}
let num = new Array(8, 50, 2, 34, 12, 8);
num.sort(eq);
let text = num.join();
document.write(text);
// student array
let students = ['John', 'Jane', 'Mary', 'Mark', 'Bob'];
// sort the array in ascending order
students.sort();
// ? result = ['Bob', 'Jane', 'John', 'Mark', 'Mary']
// sort the array in descending order
students.sort().reverse();
// ? result = ['Mary', 'Mark', 'John', 'Jane', 'Bob']
let arr = [1,2,3,3,4]
arr.sort() => sorts in ascending order from right to left
// You can sort in custom orders by defining a custom comparison function
// ex:
arr.sort(function compareNumbers(firstNumber, secondNumber){
/*
if a negative number is returned, firstNumber will be the first number in the output
if a positive number is returned, secondNumber will be the first number in the output
if a 0 is returned, it will default to returning them in the position they're already in
*/
// ascending order
// return firstNumber - secondNumber
// descending order
//return secondNumber - firstNumber
// Always want 3's to come first:
/*
if firstNumber === 3{
return -1
}
if secondNumber === 3{
return 1
}
return secondNumber - firstNumber
*/
})
Scanner sc = new Scanner(System.in);
int[] arr = new int[6];
System.out.println("Enter the elements in the array");
for (int i = 0;i< arr.length;i++){
arr[i] = sc.nextInt();
}
Arrays.sort(arr);
for (int values : arr){
System.out.print(values +" ");
}
// 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
let persolize=[ { key: 'speakers', view: 10 }, { key: 'test', view: 5 } ]
persolize.sort((a,b) => a.view - b.view);
//If it only array and not an array object, use below statement
//persolize.sort((a,b) => a - b);