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
const ascending: any= values.sort((a,b) => (a > b ? 1 : -1));
const descending: any= values.sort((a,b) => (a > b ? -1 : 1))
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
//ascending order
let ArrayOne = [1,32,5341,10,32,10,90]
ArrayOne = ArrayOne.sort(function(x,y){x-y})
//descending order
let ArrayTwo = [321,51,51,324,111,1000]
ArrayTwo = ArrayTwo.sort(function(x,y){y-x})
xxxxxxxxxx
const numbers = [4, 7, 1, 3, 6, 9, 2, 5];
const numbersSort = numbers.sort();
console.log(numbersSort);
//Output:[1, 2, 3, 4, 5, 6, 7, 9]
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
function sortArray(array) {
var temp = 0;
for (var i = 0; i < array.length; i++) {
for (var j = i; j < array.length; j++) {
if (array[j] < array[i]) {
temp = array[j];
array[j] = array[i];
array[i] = temp;
}
}
}
return array;
}
console.log(sortArray([3,1,2]));
xxxxxxxxxx
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort(); // Sorts the elements of fruits
xxxxxxxxxx
How does the following code sort this array to be in numerical order?
var array=[25, 8, 7, 41]
array.sort(function(a,b){
return a - b
})
I know that if the result of the computation is
**Less than 0**: "a" is sorted to be a lower index than "b".<br />
**Zero:** "a" and "b" are considered equal, and no sorting is performed.<br />
**Greater than 0:** "b" is sorted to be a lower index than "a".<br />
Is the array sort callback function called many times during the course of the sort?
If so, I'd like to know which two numbers are passed into the function each time. I assumed it first took "25"(a) and "8"(b), followed by "7"(a) and "41"(b), so:
25(a) - 8(b) = 17 (greater than zero, so sort "b" to be a lower index than "a"): 8, 25
7(a) - 41(b) = -34 (less than zero, so sort "a" to be a lower index than "b": 7, 41
How are the two sets of numbers then sorted in relation to one another?
Please help a struggling newbie!
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));
}
}