simon
simon

Reputation: 12912

JavaScript: sorting collection, which is not an array

I have a collection of objects in JavaScript like this:


Object collection = new Object();
collection[123] = new Item(); //another object
collection[425] = new Item();
collection[2134] = new Item();
//etc. etc.

//TODO: sort items

I would like to sort this collection according to the properties of the Item objects in collection. There is good built-in sort function in JS Array, so my first idea was to modify the code followingly:


Object collection = new Array();
collection[123] = new Item(); //another object
collection[425] = new Item();
collection[2134] = new Item();
//etc. etc.

collection.sort(comparisonFunction); //where comparisonfunction handles the sorting logic

Now the collection is nicely sorted - however, I cannot access the items using the property, which I used to set them, i.e.


collection[2134]

is null, because the JS array does not contain that many elements (and even if it would, the result would not be as expected)

What I need is the

Edit: the object does not need to behave like an array and it is not an array of nature. What I need is pretty close to Java TreeMap - a collection, which maintains a specific order, and which is also a map. Or explained in another way, I need a plain old JavaScript object (which some people know as "associative array") whose contained user-created elements can be sorted.

Edit 2: the solution I ended up using is Anatoliy's, slightly modified. Basically I create an array, where I duplicate the "collection" and map the two objects (array and collection) to get the benefits of both. Also thanks to Roatin Marth for pointing out that iterating objects are not guaranteed to happen in any kind of order (I did consider this when modifying Anatoliy's solution).

Upvotes: 1

Views: 5234

Answers (4)

Rake36
Rake36

Reputation: 1022

If you don't mind using a JS library like prototype.js...

http://api.prototypejs.org/language/hash.html

Upvotes: 0

devpl
devpl

Reputation: 204

I think that you need to separate collection into two variables with one as an array and the other as an object. You can probably create a function for your object such that you can call only one function to set both your object var and your array var.

var objects = {};
var collection = [];

objects.set = function(index, data){
  collection[index] = data;
  this[index] = data;
}

objects.set(10, new Item());
objects.set(11, new Item());

collection.sort();

Upvotes: 2

Anatoliy
Anatoliy

Reputation: 30103

Here is the untested code (just idea):

var arr = [];
for (var i in collection) {
    collection[i].index = i;
    arr.push(collection[i]);
}
arr.sort(f);
var sorted_collection = {};
for (var j in arr) {
    sorted_collection[arr[j].index] = arr[j];
}

Upvotes: 1

Roatin Marth
Roatin Marth

Reputation: 24105

Where to start...

The last point is key to your question. IMHO you need to change your data type from an Object to an Array (that can maintain a sorted order, thus the sort method available).

Upvotes: 8

Related Questions