Urutkan array berdasarkan panjang string javascript
function sortByLength(arr) {
return arr.sort((x,y) => x.length - y.length);
}
Gorgeous Giraffe
function sortByLength(arr) {
return arr.sort((x,y) => x.length - y.length);
}
array.sort(function(a, b){return b.length - a.length});
import java.util.*;
class Main
{
public static void main(String[] args)
{
ArrayList<String> arr = new ArrayList<>();
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.nextLine();
for(int i=0; i<n; i++){
arr.add(sc.nextLine());
}
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if((arr.get(i)).length() > (arr.get(j)).length()){
Collections.swap(arr,i,j);
}
}
}
System.out.println(arr);
}
}