Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Sorting Arrays

Sebastian

New Coder
hello everybody
how could I change the order of this array

Code:
var array =  ["intem1" , "intem2" , "intem3" , "intem4" , "intem5" , "intem6" , "intem7" , "intem8" , "...."];
...
var reordering = array ["intem4" , "intem3" , "intem5" , "intem1" , "intem2" , "intem6" , ""intem7" , "intem8", "...."];

the array can have a variables items numbers, but I just want to change only the position of the first 6 items, the rest remain unchanged ("intem7" , "intem8" , "....")
thank you very much
 
AFAIK there is no command to sort only part of an array. I would slice the array in two, one with the first 6 items, the other with the remaining items, sort the first array normally, then paste the two back together again.
 
AFAIK there is no command to sort only part of an array. I would slice the array in two, one with the first 6 items, the other with the remaining items, sort the first array normally, then paste the two back together again.
good ideea:thumbsup:
thank you!

Code:
var array=["item1" , "item2" , "item3" , "item4" , "item5" , "item6" , "item7" , "item8", "item9", "item10", "intem11"]
var arrayLength= (array.length)
var part1 = array.slice(0, 5);
var part2 = array.slice(5, arrayLenght);
alert([part1[3],part1[2],part1[4],part1[0],part1[1], part2]);
 
JavaScript:
const arr = ["item1" , "item2" , "item3" , "item4" , "item5" , "item6" , "item7" , "item8"],
         order = [3, 2, 4, 0, 1],
         reodered = arr.map( (n, i, ar) => i < order.length ? ar[ order[i] ] : n );
  
   console.log( reodered ); //  ['item4', 'item3', 'item5', 'item1', 'item2', 'item6', 'item7', 'item8']
 
JavaScript:
const arr = ["item1" , "item2" , "item3" , "item4" , "item5" , "item6" , "item7" , "item8"],
         order = [3, 2, 4, 0, 1],
         reodered = arr.map( (n, i, ar) => i < order.length ? ar[ order[i] ] : n );
 
   console.log( reodered ); //  ['item4', 'item3', 'item5', 'item1', 'item2', 'item6', 'item7', 'item8']
An ingenious solution, although rather arcane (I'd almost say obfuscated). A simple task should be programmed simply. But here speaks an old time programmer 😀
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom