views:

148

answers:

2

I have a javascript hash table, like so:

var things = [ ];
things["hello"] = {"name" : "zzz I fell asleep", "number" : 7};
things["one"] = {"name" : "something", "number" : 18};
things["two"] = {"name" : "another thing", "number" : -2};

I want to sort these into order by name, so if I iterate through the hash table it will go in order

another thing
something
zzz I fell asleep

I tried doing this:

function compareThings(thing1, thing2) {
    var name1 = thing1["name"].toLowerCase();
    var name2 = thing2["name"].toLowerCase();
    if (name1 < name2) {
        return -1;
        }
    if (name1 > name2) {
        return 1;
        }
    return 0;
}

things.sort(compareThings);

But it doesn't seem to work.

Edit: it occurs to me that perhaps a sorted hash table is an oxymoron. If so, what's the best way to get access to a sorted list of the things here?

+1  A: 

your parameters are thing1 and thing2, but you're referencing some variables called asp1 and asp2, which, as far as I can tell from the source you've provided, do not exist.

Also, I think what you're looking for is an associative array, which is not instantiated with the [] syntax. See here for more info:

http://www.quirksmode.org/js/associative.html

EDIT: I don't think there's an array in Javascript that'll let you do what you want.

You can have plain old array, which will let you do the custom sorting or you can have an associative array, which will let you have the named values.

With the regular array, you can obviously iterate through the indexes.

With the associative array, you can iterate through the names by doing for (var key in myArray)

Daniel Schaffer
Sorry, typo....
Colen
Be careful with that for...in syntax. It'll loop over *all* the properties the object in question has, which includes things other than array keys.
Syntactic
^^ This is true, but shouldn't be an issue as long as it doesn't inherit from anything.
Daniel Schaffer
+1  A: 

If you want to iterate through a hash table in JavaScript in order, make an array, populate it with the hash keys, and then sort it.

<html>
<body>
<pre>
  <script>
    var things = new Object ();
    things["hello"] = {"name" : "zzz I fell asleep", "number" : 7};
    things["one"] = {"name" : "something", "number" : 18};
    things["two"] = {"name" : "another thing", "number" : -2};
    var keys = [];
    for (var key in things) {
      if (things.hasOwnProperty(key)) {
        keys.push(key);
      }
    }
    keys.sort ();
    for (i in keys) {
      var key = keys[i];
      var value = things[key];
      document.write (key +"="+value+"\n");
    }
  </script>
</pre>
</body>
</html>
Kinopiko
Just a note, the order of iteration of the [`for...in`](https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Statements/for...in) statement can be arbitrary, there is nothing described in the [ECMAScript specification](http://bclary.com/2004/11/07/#a-12.6.4) about the order of enumeration of the properties, it is implementation dependent... no guarantees, use with care...
CMS
@CMS: So I should use `for (var i = 0; i < keys.length; i++)` to be strictly correct?
Kinopiko
CMS
Thanks for the links.
Kinopiko