0

I have a number of variables with values:

apples_count = 1;
oranges_count = 4;
bananas_count = 2;

I want to store them in an array (eg fruits = []) so I can order them by value, ie

[oranges_count, bananas_count, apples_count]

If I attempt to add the variable oranges_count using

fruits.push(oranges_count);

Then the variable value 4 is added but not the variable itself.

How can I store the variables in an array so I can sort them by value?

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
haz
  • 740
  • 1
  • 11
  • 20
  • 1
    JavaScript doesn't have variable references. You can only store values. – Barmar Jan 23 '20 at 07:47
  • 1
    Why can't you sort the array by value when you store the values in the array? What are you really trying to accomplish? – Barmar Jan 23 '20 at 07:48
  • +1 thanks for your suggestion - I'm aiming to order the placement of page elements but I found it strange I couldn't do this in an intuitive way – haz Jan 23 '20 at 07:51
  • 1
    [JavaScript is always pass-by-value](https://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language), so you can't sort "vairables", only their values. If you need to associate some *label* with a value, you can use an object or a Map or even an array of pairs or objects that will have key-value relationship. – VLAZ Jan 23 '20 at 07:51

3 Answers3

2

Don't use separate variables, use an array of objects.

const fruits = [
  {type: "apple", count: 1},
  {type: "orange", count: 4},
  {type: "banana", count: 2}
];

fruits.sort((a, b) => a.count - b.count);
console.log(fruits);
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

You have 3 standalone variables with similar functionality. Consider using an object or an array instead, stored in a single variable. Then you can use array methods on the array to sort and retrieve the ordered counts, eg:

const counts = {
  apples: 1,
  oranges: 4,
  bananas: 2,
};
const sortedEntries = Object.entries(counts).sort((a, b) => a[1] - b[1]);
for (const entry of sortedEntries) {
  console.log(entry);
}

With the above, to change a particular count, just do something like counts.apples = 5.

Or, using the array method from the beginning:

const fruits = [
  { name: 'apple', count: 1 },
  { name: 'orange', count: 4 },
  { name: 'banana', count: 2 },
];
const sortedFruits = fruits.sort((a, b) => a.count - b.count);
for (const fruit of sortedFruits) {
  console.log(fruit);
}

To change the count of an object in the above array, either .find the object with the name you want and assign to its count property:

fruits.find(({ name }) => name === 'apple').count = 10;

Or create an object like above, mapping each fruit name to its object.

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
1

Store an object instead of only the value. For example, you could use something like this:

var apples = {
  amount: 1,
  name: 'apple',
  type: 'fruit'
};
var oranges = {
  amount: 4,
  name: 'orange',
  type: 'fruit'
};
var bananas = {
  amount: 2,
  name: 'banana',
  type: 'fruit'
};
var collection = [
  apples,
  oranges,
  bananas
];
var sorted_collection = collection.sort(( first, second ) => first.amount - second.amount );

console.log( sorted_collection );
Shilly
  • 8,511
  • 1
  • 18
  • 24