Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am new to this board have been fishing for info here for a long time and this is my first time posting here. I am in sort of a jam right now I have an array in javascript that looks like this. Each ID is accociated with a persons name

        Array
         (
            [230] => Smith, Marc-Andre 
            [11369] => Smith, Wayne 
            [12561] => Smith, Diane 
            [12637] => Smirnova, Natalie
         )

Right now the array is sorted by the index which is the ID of the person. I am wondering if its possible to sort it alphabetically such that the array now looks like

          Array
          (
             [12637] => Smirnova, Natalia 
             [12561] => Smith, Diane 
             [230] => Smith, Marc-Andre 
             [11369] => Smith, Wayne       
          )

So far I tried using array.sort() it does sort alphabetically but the ID is lost it gives me

      Array
          (
             [0] => Smirnova, Natalia 
             [1] => Smith, Diane 
             [2] => Smith, Marc-Andre 
             [3] => Smith, Wayne       
          )

This is my first time programming javascript so thanks for all the help in advance.

share|improve this question
 
As a note, you should be aware that you are creating an array that is (max index + 1) in length when you do this. So, for this example, it's 12638 elements long. –  Marc Jul 8 '11 at 23:35
 
Actually, that's incorrect. (Most) Javascript (implementations) use sparse arrays. Array = hashtable, basically. –  wmorrell Jul 8 '11 at 23:39
 
@wmorrell @Marc Either of you have documentation on this? I had never considered this and am interested. Thanks. –  Wiseguy Jul 8 '11 at 23:41
 
 
@wmorrell: you're absolutely right, someone just forgot to tell .length that bit of information, always throws me off that it's biggest index + 1 internally. –  Marc Jul 8 '11 at 23:47
add comment

3 Answers

Store the entries as objects in an array, with ID and name properties. The JSON would look like:

[ {'id':230,'name':'Smith, Marc-Andre'}, {'id':11369,'name':'Smith, Wayne'}, {'id':12561,'name':'Smith, Diane'}, {'id':12637,'name':'Smirnova, Natalie'} ]

Then pass a function into Array.sort() to choose which property to sort on. See https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/array/sort

share|improve this answer
add comment

You can do something like:

function arrayify(arg) {
    var ret = [];
    for (var i in arg) {
        if (!arg.hasOwnProperty(i) continue;
        ret.push({key: i, value: arg[i]});
    }
    return ret;
}

arr = arrayify(arr).sort(function(a, b) {
    return a.key.toString() - b.key.toString();
});
share|improve this answer
add comment

You can't use a simple array to do what you're asking for. An array is, by definition, a list of data that is ordered by the array indexes. You are asking for it to be ordered by something other than the array indexes. I think the simplest way to store it would end up like this:

   var myData = [
        [12637, "Smirnova, Natalia"], 
        [12561, "Smith, Diane"], 
        [230, "Smith, Marc-Andre"], 
        [11369, "Smith, Wayne"]
    ];

So, it would be an array of arrays, where each sub-array contains two items, the first is the id, the second is the name. This is a similar, but slightly different form what wmorrell proposed.

You can then sort the data by the last name by passing a custom comparision into the sort routine like this:

// custom sort (assumes all data is fully formed)
// sorts in ascending order by the 2nd item in each embedded array
    myData.sort(function(a,b) {
      if (a[1] < b[1]) return(-1);
      if (a[1] > b[1]) return(1);
      return(0); 
    });
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.