A: 

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
ŁukaszW.pl
A: 

Javascript code with random function:

var index = Array();
for(var i = 0; i < fruits.length; i++){
   index.push(0);
}

var newfruits = Array();
for(var i = 0; i < fruits.length; i++){
   var n = Math.random()*fruits.length;
      while(index[n] == 1){
      n = Math.random()*fruits.length;
   }
   newfruits.push(fruits[n]);
   index[n] = 1;
}
Miro
+5  A: 

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.

jmar777
I'll get back to you asap.Thank you.
Faili
A: 

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.

Joel Coehoorn
A: 
Faili