xxxxxxxxxx
function reverseAlphabeticalOrder(arr) {
// Add your code below this line
return arr.sort(function(a, b) {
return a === b ? 0 : a < b ? 1 : -1;
});
// Add your code above this line
}
reverseAlphabeticalOrder(["l", "h", "z", "b", "s"]);
// Returns ['z', 's', 'l', 'h', 'b']
xxxxxxxxxx
function alphabeticalOrder(arr) {
// Add your code below this line
return arr.sort(function(a, b) {
return a === b ? 0 : a < b ? -1 : 1;
});
// Add your code above this line
}
alphabeticalOrder(["a", "d", "c", "a", "z", "g"]);
xxxxxxxxxx
// Java Program to Sort Names in an Alphabetical Order
import java.io.*;
class GFG {
public static void main(String[] args)
{
// storing input in variable
int n = 4;
// create string array called names
String names[]
= { "Rahul", "Ajay", "Gourav", "Riya" };
String temp;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
// to compare one string with other strings
if (names[i].compareTo(names[j]) > 0) {
// swapping
temp = names[i];
names[i] = names[j];
names[j] = temp;
}
}
}
// print output array
System.out.println(
"The names in alphabetical order are: ");
for (int i = 0; i < n; i++) {
System.out.println(names[i]);
}
}
}