Q:

Write a JavaScript program to sort the strings of a given array of strings in the order of increasing lengths

0

Write a JavaScript program to sort the strings of a given array of strings in the order of increasing lengths.  
Note: Do not change the order if the lengths of two string are same.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

function sort_by_string_length(arra) {
	for (var i = 0; i < arra.length; i++) {
		for (var j = i + 1; j < arra.length; j++) {
			if (arra[i].length > arra[j].length) {
				var m = arra[i];
				arra[i] = arra[j];
				arra[j] = m;
			}
		}
	}
	return arra;
}
var arra = ["xyz","acd","aa","bb","zzz","", "a","b"];
console.log("Original array: "+ arra+"\n");
console.log(sort_by_string_length(["xyz","acd","aa","bb","zzz","", "a","b"]));

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now