xxxxxxxxxx
//there are numerous methods to sort an array
//A simpler Method
let array = [4,2,3,1,5,7,9,8];
array.sort(function(a, b){
return a-b;
})
//A different approach
let min = 0;
let overWriteArray =[];
let array = [4,2,3,1,5,7,9,8];
let length = array.length
let store = 0;
let temp = 0;
for (let y = 0; y<length; y++){
min = Math.min(array);
temp = array.indexOf(min);
store = array.splice(temp, 1);
store = parseInt(store.join());
overWriteArray.push(store);
}
console.log(overWriteArray);
xxxxxxxxxx
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;
}
}
}
xxxxxxxxxx
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort(); // Sorts the elements of fruits
xxxxxxxxxx
var l = ["a", "w", "r", "e", "d", "c", "e", "f", "g"];
console.log(l.sort())
/*[ 'a', 'c', 'd', 'e', 'e', 'f', 'g', 'r', 'w' ]*/
xxxxxxxxxx
let sortArray = array.sorted(by: { $0.name.lowercased() < $1.name.lowercased() })
xxxxxxxxxx
import java.util.Scanner;
public class ArraySorting {
public static int[] inputArray() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of terms in the input array-");
int noOfTerms = sc.nextInt();
int[] array = new int[noOfTerms];
System.out.println("Enter the terms in the input array-");
for (int i = 0; i < noOfTerms; i++) {
array[i] = sc.nextInt();
}
return array;
}
public static int[] upSort(int[] b) {
// Ascending order
for (int i = 0; i < b.length - 1; i++) {
for (int j = 0; j < b.length - i - 1; j++) {
if (b[j] > b[j + 1]) {
// Swap elements if they are in the wrong order
int tmp = b[j];
b[j] = b[j + 1];
b[j + 1] = tmp;
// Comment: Elements swapped successfully
}
}
}
return b;
}
public static int[] downSort(int[] a) {
// Descending order
for (int i = 0; i < a.length - 1; i++) {
for (int j = 0; j < a.length - i - 1; j++) {
if (a[j] < a[j + 1]) {
// Swap elements if they are in the wrong order
int tmp = a[j];
a[j] = a[j + 1];
a[j + 1] = tmp;
// Comment: Elements swapped successfully
}
}
}
return a;
}
public static void printArray(int[] array) {
for (int element : array) {
System.out.print(element + " ");
}
}
public static void main(String[] args) {
int[] a = inputArray();
System.out.println("Array in ascending order:");
printArray(upSort(a));
System.out.println();
System.out.println("Array in descending order:");
printArray(downSort(a));
}
}
xxxxxxxxxx
// Java program to Sort a Subarray in Descending order
// Using Arrays.sort()
// Importing Collections class and arrays classes
// from java.util package
import java.util.Arrays;
import java.util.Collections;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Note that we have Integer here instead of
// int[] as Collections.reverseOrder doesn't
// work for primitive types.
Integer[] arr = { 13, 7, 6, 45, 21, 9, 2, 100 };
// Sorts arr[] in descending order using
// reverseOrder() method of Collections class
// in Array.sort() as an argument to it
Arrays.sort(arr, Collections.reverseOrder());
// Printing the array as generated above
System.out.println("Modified arr[] : "
+ Arrays.toString(arr));
}
}
xxxxxxxxxx
// Java program to Sort a Subarray in Descending order
// Using Arrays.sort()
// Importing Collections class and arrays classes
// from java.util package
import java.util.Arrays;
import java.util.Collections;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Note that we have Integer here instead of
// int[] as Collections.reverseOrder doesn't
// work for primitive types.
Integer[] arr = { 13, 7, 6, 45, 21, 9, 2, 100 };
// Sorts arr[] in descending order using
// reverseOrder() method of Collections class
// in Array.sort() as an argument to it
Arrays.sort(arr, Collections.reverseOrder());
// Printing the array as generated above
System.out.println("Modified arr[] : "
+ Arrays.toString(arr));
}
}
xxxxxxxxxx
// Java program to Sort a Subarray in Descending order
// Using Arrays.sort()
// Importing Collections class and arrays classes
// from java.util package
import java.util.Arrays;
import java.util.Collections;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Note that we have Integer here instead of
// int[] as Collections.reverseOrder doesn't
// work for primitive types.
Integer[] arr = { 13, 7, 6, 45, 21, 9, 2, 100 };
// Sorts arr[] in descending order using
// reverseOrder() method of Collections class
// in Array.sort() as an argument to it
Arrays.sort(arr, Collections.reverseOrder());
// Printing the array as generated above
System.out.println("Modified arr[] : "
+ Arrays.toString(arr));
}
}