+ 7
Array sorting.
Suppose I have an array var arr = ["a","b","c","d","e","f"] and I want to rearrange the elements of using for loop. (not using sort() method). Example: for i = 1, arr = ["a","b","c","d","e","f"] i = 2 , arr = ["b","c","d","e","f","a"] i = 3, arr = ["c","d","e","f","a","b"] .......................... i = 6, arr = ["f","a","b","c","d","e"] How can I do this? Thanks for giving your valuable time to answer.
6 Answers
+ 7
Maybe something like this:
function sortMyArr(arr, count) {
for(let i = 0; i < count; ++i) {
arr.push(arr[0]);
arr = arr.slice(1);
}
return arr;
}
let arr = ["a","b","c","d","e","f"];
arr = sortMyArr(arr, 3);
alert(arr);
+ 11
here's my solution in python..
it creates an array randomly and sort.
https://code.sololearn.com/cfqGXEqaI37B/?ref=app
+ 2
Thank you @ChaoticDawg.
+ 2
@kazi @justin I needed this solution in Javascript.
Anyway thanks for stretching your helping hands. I'm learning python now. Hope your code will help me in near future.
0
tagged