Apollo
Apollo

Reputation: 9054

Simple sort algorithm for names - Javascript

If I have an array of objects Array 1:[Object 1, Object 2, Object 3] and each object looks like the following:

object 1 has properties : creation date, name, class, size where name = Bear
object 2 has properties : creation date, name, class, size where name = Dog
object 3 has properties : creation date, name, class, size where name = Guerilla

And I want to sort these objects based upon their respective names in alphabetical order, so Array 2 looks like the following [Object 1 (Bear), Object 2 (Dog), Object 3 (Guerilla)]

how would I approach this problem in Javascript? Are there any convenience methods for doing this? I am relatively new to JS. Thank you in advance!

Upvotes: 0

Views: 1142

Answers (5)

Guffa
Guffa

Reputation: 700322

Use the sort method with a custom comparer:

Array1.sort(function(x, y){
  var n1 = x.name;
  var n2 = y.name;
  return n1 == n2 ? 0
    : n1 < n2 ? -1
    : 1;
});

Upvotes: 1

cdn
cdn

Reputation: 1422

The javascript sort method accepts an anonymous comparison function that gives you all the flexibility that you would need to sort objects on any attribute.

The general pattern is:

arr.sort(function(a,b){...});

The "anonymous" function provided to the sort method accepts two objects to compare, and should return a value that indicates which element to sort higher, as follows:

if a should be sorted higher than b, return 1
if a should be sorted lower than b, return -1
if a is equivalent to b (for sorting purposes), return 0

Here's an example (ellipses indicate other attributes):

var beasties = [
  {name:'Bear', created_at: ... },
  {name:'Dog', ... },
  {name:'Gorilla', ... }
];

beasties.sort(function(a,b){return a.name > b.name});

Here, I'm relying on the ability for js to compare the strings that are returned by the name attribute.

I think that ought to do the trick for you.

Upvotes: 1

Yoshi
Yoshi

Reputation: 54649

arr.sort(function (a, b) {
  return a.localeCompare(b);
});

Note that sort is an in-place sort, so your original array will be sorted.

Upvotes: 1

slashingweapon
slashingweapon

Reputation: 11307

You can use the sort method of the array, and pass in a function:

Array1.sort(function(left,right) {
    if (left.name < right.name) return -1;
    if (left.name > right.name) return 1;
    return 0;
});

Upvotes: 0

Philipp
Philipp

Reputation: 69663

You can pass a custom compare function to Array.sort which will then be used by the sorting algorithm to decide which one of two array elements is larger. When the algorithm compares two objects, it passes these to the compare function. When the function returns 1, it considers the first one as larger. When it returns -1, it considers the second one larger. When it returns 0, it considers them equal.

 function compare_function(obj1, obj2){
      if (obj1.name == obj2.name) return 0;
      if (obj1.name < obj2.name) return -1 else return 1;

  }
  my_array.sort(compare_function) 

Upvotes: 0

Related Questions