sort() orders the elemts alphabetically:
var fruits = new Array("Apple", "Banana", "Kiwi", "Ananas", "Mango");
Namen.sort();
document.write(fruits);
Well, I don’t want the alphabetical order…it should be like
var reorderFruits = new Array("Kiwi", "Apple", "Mango", "Banana", "Ananas");
Is it possible to reorder the order the way I imagined it? How?
Thank you in advance.
Faili
Edit:
<input type="checkbox" id="apple" checked="checked" />
<label for="apple">Apple</label>
<input type="checkbox" id="Banana" checked="checked" />
<label for="Banana">Banana</label>
<input type="checkbox" id="Kiwi" />
<label for="Kiwi">Kiwi</label>
<input type="checkbox" id="Mango" checked="checked"/>
<label for="Mango">Mango</label>
Well… I get the information from a website I have no access to. No, I want to reorder the order.
In this example it is:
Apple -> Banana -> Kiwi -> Mango
Now I’m asking whether it is possible to make it to:
Kiwi -> Banana -> mango -> Apple
Please ask whether there is sth unclear.
Thank you so much.
This is just an example that will sort the array by the length of the item. It will match your desired output on any browser that uses a stable sorting algorithm (which is most of them now). You can modify the function to sort by any other criteria you want as well.
There are some rules with this. The sort function must compare a and b such that:
I mention all this because one common (wrong) use for this is to randomize or shuffle an array such the sort function returns a random value. That will appear to work, but isn’t really random and can cause bugs later.