简体   繁体   English

名称的简单排序算法-Javascript

[英]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: 如果我有一个对象数组Array 1: [Object 1, Object 2, Object 3] ,则每个对象如下所示:

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)] 而且我想根据它们各自的名称(按字母顺序)对它们进行排序,因此数组2看起来如下[Object 1 (Bear), Object 2 (Dog), Object 3 (Guerilla)]

how would I approach this problem in Javascript? 我将如何用Javascript处理此问题? Are there any convenience methods for doing this? 有什么方便的方法吗? I am relatively new to JS. 我对JS比较陌生。 Thank you in advance! 先感谢您!

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;
});
arr.sort(function (a, b) {
  return a.localeCompare(b);
});

Note that sort is an in-place sort, so your original array will be sorted. 请注意, sort就地排序,因此原始数组将被排序。

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. javascript sort方法接受匿名比较功能,该功能为您提供了对任何属性上的对象进行排序所需的所有灵活性。

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: 提供给sort方法的“ anonymous”函数接受两个对象进行比较,并应返回一个值,该值指示对哪个元素进行更高排序,如下所示:

if a should be sorted higher than b, return 1 如果a的排序应高于b,则返回1
if a should be sorted lower than b, return -1 如果a的排序应低于b,则返回-1
if a is equivalent to b (for sorting purposes), return 0 如果a等于b(用于排序),则返回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. 在这里,我依靠js来比较name属性返回的字符串的功能。

I think that ought to do the trick for you. 我认为应该为您解决这个问题。

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. 您可以将自定义比较函数传递给Array.sort,然后排序算法将使用该比较函数来确定两个数组元素中哪个较大。 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. 当该函数返回1时,它将第一个视为更大。 When it returns -1, it considers the second one larger. 当返回-1时,它认为第二个较大。 When it returns 0, it considers them equal. 当它返回0时,认为它们相等。

 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) 

You can use the sort method of the array, and pass in a function: 您可以使用数组的sort方法,并传入一个函数:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM