+ 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.

20th Dec 2017, 8:05 AM
Suresh
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);
20th Dec 2017, 8:35 AM
ChaoticDawg
ChaoticDawg - avatar
20th Dec 2017, 1:33 PM
Justine Ogaraku
Justine Ogaraku - avatar
+ 11
here's my solution in python.. it creates an array randomly and sort. https://code.sololearn.com/cfqGXEqaI37B/?ref=app
20th Dec 2017, 10:10 AM
KĂ€zĂź MrĂźdĂčl HĂžssÀßn
KĂ€zĂź MrĂźdĂčl HĂžssÀßn - avatar
+ 2
Thank you @ChaoticDawg.
20th Dec 2017, 6:55 PM
Suresh
+ 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.
20th Dec 2017, 6:59 PM
Suresh
0
tagged
20th Dec 2017, 9:20 PM
Rabee Abbas
Rabee Abbas - avatar