Array Ordering

How can I order an array?

For example, I have an array like this:

var food : String = [“Cheese”, “Butter”];

and another like:

var prices : float = [10.99, 2.5];

How can I list them like cheese then butter (price) but not butter then cheese (alphabetic)?

To start with you shouldn’t be using two arrays like that if you want to keep the items together - instead make a class:

class FoodAndPrice
       var food : String;
       var price : float;

FoodAndPrice[] items;

Then add those to a list or an array (this means when you sort they will be kept together).

To sort it by one of the values you can use Linq:

 import System.Linq;

The to get the sorted items into an array from an array or list:

  var sortedItems = items.OrderBy(function(i) i.price).ToArray();

  var cheapestItemName = sortedItems[0].food;
  var cheapestItemPrice = sortedItems[0].price;

you can use the Sort() function in the JavaScript

it will sort them in alphabetical or ascendant order.

for more info you can check

This has been already answered here:

Or you culd store your data in Lists instead of builtin arrays and apply the sort() method.