2

I have an array of objects and I was wondering if you could sort the array by an attribute in the object? Here is a sample of what I mean:

for (var i = 0; i < response.data.length; i++) {
    person[i] = {
        "name":response.data[i].name,
        "phone":response.data[i].phone
    };
    person[i].name.sort();
}

I'm trying to sort the array by the objects name.

Dennis Martinez
  • 6,344
  • 11
  • 50
  • 67
  • `person[i].name.sort()` will only work if `response.data[i].name` is an array. And then it will only sort the names of one person. Have a look at http://stackoverflow.com/search?q=javascript+sort+array+of+objects – Felix Kling Jun 16 '11 at 12:24
  • possible duplicate of [Sort array of objects](http://stackoverflow.com/questions/5876424/sort-array-of-objects) – Felix Kling Jun 16 '11 at 12:26

2 Answers2

8
person.sort(function(a, b) {
  if (a.name < b.name) return -1;
  if (a.name > b.name) return 1;
  return 0;
});

sort takes a comparator function as an optional parameter.

Amadan
  • 191,408
  • 23
  • 240
  • 301
0

Sure. The array object has a sort function that allows you to pass in a function to determine how to compare an object.

//Passes in A and B, which are the objects in your array.
function sortName(a, b)
{
    return a.name <= b.name ? -1 : 1
           //return integer
           //negative number a is less than b
           //positive number b is less than a
           //zero a and b are equal
           //(My function doesn't ever return zero)
}

person.sort(sortName);
MillsJROSS
  • 1,239
  • 7
  • 9
  • Though it should work, my nitpicking for the day is that your comments don't match your code insofar as `//zero a and b are equal` will never actually happen. – Wiseguy Jun 16 '11 at 12:35
  • It's more of an instruction on what's required in the comparator function. A teach a man to Fish type a thing. I'll edit to clarify in the code. – MillsJROSS Jun 16 '11 at 12:38
  • If that's what's required, then why not do that? :-) Since you're sorting here, it should still work correctly. But if you used the same comparison callback for something else, that operation may yield incorrect results since the callback does not do what's expected. – Wiseguy Jun 16 '11 at 12:40
  • Because that's how I coded it :) If I named the function compareName, I probably would have use the zero, but my function is specific to the array sort function. – MillsJROSS Jun 16 '11 at 12:54
  • Perhaps that's fair enough, but in principle I still say you code things right, not just right enough. – Wiseguy Jun 16 '11 at 13:03
  • 1
    specific to the sort function, the standard says that if the comparator function returns 0, the sort should be stable (i.e. the "equal" elements should not change positions relative to each other). Some browsers do not implement this, but I still think it's nice to have the full comparator implementation. https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/sort – Amadan Jun 16 '11 at 14:47