Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In one of your comments you asked the difference between the splice method and the push+sort method.</p> <p>Splice just cuts up your array and inserts/deletes values based on index. Using above mentioned example:</p> <pre><code>var strings = ["14S", "16S", "19S"]; strings.splice(1,0,"15S"); </code></pre> <p>This will literally place the "15S" on index 1. This is why there is a remark "If you don't know the position you want to insert the element into:"</p> <p>In which case the push+sort method comes into play. At this point you don't have to go about counting your elements and making sure you place everything at the right location. You literally just push your "15S" at the end of the array, and then have the sort() automatically sort everything for you.</p> <p>This, granted that your array is indeed sortable. Some setups don't really allow you to sort your arrays (Like colors sorted by color in the rainbow ... you can't just sort them using sort()).</p> <p>In this case KooiInc's response comes into play. At this point you know where your item needs to be. 15S comes after 14S, so you search for 14S, get that index, and use that index to splice. At this point you still have to calculate yourself what element 15S will have to go after - in my rainbow example you'll have to actively remember where you want to 'insert' which color based on which colors are in your array at this moment.</p> <p>In your example the values seem to be suited for normal sorting, so i would go with the Push+sort approach a few people mentioned.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload