Apple, Banana, Mango, Ananas and Kiwi - Can I reorder the order?
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.
It's not clear from your example what sort order you want, but it is possible to provide your own compare function to the sort() method, which will allow for arbitrary sort orders. Here's an example that will do "reverse alphabetical":
var fruits = new Array("Apple", "Banana","Kiwi", "Ananas", "Mango");
fruits.sort(function(x, y) {
if (x > y) return -1;
if (x < y) return 1;
return 0;
});
But again, from your example I'm not sure what the compare function should look like in your case.
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.
var fruits = new Array("Apple", "Banana","Kiwi", "Ananas", "Mango");
fruits.sort(function(a,b) {
return a.length - b.length;
});
document.write(fruits);
There are some rules with this. The sort function must compare a and b such that:
- It returns a numeric value indicated whether a is less than, equal to, or greater than b
- Multiple comparisons between the same a and b can happen, and should always return the same result for the same a and b.
- Equal values must always return 0.
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.
Of course it is. For example you can add them in the order as you like, then the order will be kept. On the other hand if you have them in some order and you want to reorder them as you like then you can change them just like you do with changing values of two variables f.e.
var fruits = new string[3] { "Apple", "Banana", "Ananas" };
var tempFruit= fruits[0]; // hold fruit in a temp variable
fruits[0] = fruits[1]; // exchange fruit positions
fruits[1] = tempFruit; // retrieve fruit from position 0 from temp variable
Well,
maybe I didn't explain my problem good enough. Here's the solution that worked out for me.
Thank you though. Without you, I wasn't able to create the following:
var newOrder = ['Kiwi', 'Apple', 'Mango', 'Banana', 'Ananas'];
for (var i = 0; i < newOrder.length - 1; i++) {
var section = "label[for='" + newOrder[i] + "']";
var section_2 = "#" + newOrder[i];
$(section).insertAfter(section_2);
var section_3 = "#" + newOrder[i + 1];
$(section_3).insertAfter(section);
}
var section = "label[for='" + newOrder[newOrder.length - 1] + "']";
var section_2 = "#" + newOrder[newOrder.length - 1];
$(section).insertAfter(section_2);
Well, I had to use the label-tag. It was necessary to have a correct name after the checkbox. Some checkboxes were checked, others not.
Thank you.
精彩评论