简体   繁体   English

当数组的某些部分没有数据时,javascript自定义排序

[英]javascript custom sort when parts of an array has no data

I have an object model that looks like this: 我有一个如下所示的对象模型:

MyObject = {
   TheName: "",
   OtherProps :....
}

I have an array of these objects and my custom sort function looks like this: 我有一个这些对象的数组,我的自定义排序函数如下所示:

function SortArray() {

  var CustomFunction;

  var CustomSortAsc = function (a, b) {
    return a['TheName'] - b['TheName'];
  }

  var CustomSortDsc = function (a, b) {
    return b['TheName'] - a['TheName'];
  }

  if (SomeCondition) {
      CustomFunction = CustomSortAsc;
  } else {
      CustomFunction = CustomSortDsc;
  }

  SomeArray.sort(CustomFunction);
}

SomeArray has somewhere around 200-300 objects in it and the problem is that sometimes the object has an empty TheName property. SomeArray有大约200-300个对象,问题是有时对象有一个空的TheName属性。 Because of that, the sort doesn't seem to work as expected. 因此,排序似乎没有按预期工作。 For instance, if there are values then sort by name and then put all the values for which there are no names according to the sort function. 例如,如果有值,则按名称排序,然后根据sort函数放入没有名称的所有值。

What's the way to make this work? 这项工作的方法是什么? Thanks for your suggestions. 谢谢你的建议。

As your TheNames are strings, you probably better use localeCompare instead: 由于您的TheNames是字符串,您最好使用localeCompare

var CustomSortAsc = function(a, b) {
    return (a['TheName'] || '').localeCompare(b['TheName'] || '');
};

And I'll probably write it all like this: 我可能会这样写:

var baseSort = function(a, b) { 
  return (a['TheName'] || '').localeCompare(b['TheName'] || '');
};
var CustomFunction = SomeCondition 
                   ? baseSort 
                   : function(a, b) { return -baseSort(a, b); };

UPDATE: and if you need to see the empty values the last all the time... 更新:如果你需要一直看到空值...

var CustomSortAsc = function(a, b) {
    return a['TheName'] === '' 
         ? b['TheName'] === ''
           ? 0
           : 1
         : b['TheName'] === ''
           ? -1
           : a['TheName'].localeCompare(b['TheName']); 
};

... or, if you prefer if : ...或者,如果您愿意, if

if (a['TheName'] === '') {
  if (b['TheName'] === '') {
    return 0;
  } else {
    return 1;
  }
} else {
  if (b['TheName'] === '') {
    return -1;
  } else {
    return a['TheName'].localeCompare(b['TheName']);
  }
}

(is this actually more readable?) (这实际上更具可读性吗?)

A custom sort function must return a number (<,= or > 0). 自定义排序函数必须返回一个数字(<,=或> 0)。 If it returns anything else (like NaN ), the behaviour is "implementation specific" (according to the standard), which means it usually breaks and just sorts nothing. 如果它返回任何其他内容(如NaN ),则行为是“特定于实现”(根据标准),这意味着它通常会中断并且不会对任何内容进行排序。

I'm not sure what you are trying to sort for ('TheName' sounds like strings, your function is for numbers), but you should just return 1 if the property does not exist on b and -1 if on a; 我不确定你要排序的是什么('TheName'听起来像字符串,你的函数是数字),但是如果b上不存在属性你应该返回1,如果是a则你应该返回-1; then your property-less items will be sorted to the end of the array. 然后,您的无属性项将被排序到数组的末尾。

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

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