为什么compareFunction必须考虑负数? [英] Why does compareFunction has to consider negative?

查看:122
本文介绍了为什么compareFunction必须考虑负数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Array.prototype.sort()

compareFunction(a, b)中,仅当我们需要交换a和b的位置时,我们才返回正值.

In compareFunction(a, b), only when we need to exchange a and b's position, we return a positive value.

如果省略了compareFunction中的负数if-statement,则Array.prototype.sort()仍然有效,那么开发人员为什么要编写if-statement并返回负值?

If negative if-statement in compareFunction is omitted, the Array.prototype.sort() still works, so why should developers write if-statement which returns a negative value?

var list = [4, 5, 3, 5, 6, 9, 1, 4, 2];
list = list.sort(function(a, b) {
  if (a > b) {
    return 1;
  }
});
console.log(list); // correct result

推荐答案

这里的主要问题是您发明了自己的比较函数定义,并以此为基础提出了问题:

The main problem here is that you've invented your own definition of the comparison function and are basing your question off of that:

在compareFunction(a,b)中,仅当我们需要交换a和b的位置时,我们才返回一个正值.

In compareFunction(a, b), only when we need to exchange a and b's position, we return a positive value.

这是不正确的. 何时需要交换a和b的位置"是一个实现细节,您正在使实现与接口混淆.

This is incorrect. "When we need to exchange a and b's position" is an implementation detail, and you are confusing implementation with interface.

compareFunction不负责指示何时应交换两个元素.它负责准确传达两个元素的关系.排序算法对该信息的处理方式取决于实现者.如果您有时仅返回正确的值,那么就不可能一直都期望得到正确的结果.

The compareFunction is not responsible for indicating when two elements should be swapped. It is responsible for accurately conveying the relationship of two elements. What the sort algorithm does with that information is up to the implementer. If you only return the correct value some of the time, then you can't expect a correct result all of the time.

例如,排序实现者可以实现这样的排序(基于

For example, a sort implementer could implement the sort like this (based off the example at https://www.nczonline.net/blog/2012/09/17/computer-science-in-javascript-insertion-sort/). If I run it with a valid comparison function, it produces the correct result:

function insertionSort(items, compare) {

  var len = items.length, // number of items in the array
    value, // the value currently being compared
    i, // index into unsorted section
    j; // index into sorted section

  for (i = 0; i < len; i++) {

    // store the current value because it may shift later
    value = items[i];

    for (j = i - 1; j > -1 && compare(value, items[j]) < 0; j--) {
      items[j + 1] = items[j];
    }

    items[j + 1] = value;
  }

  return items;
}

console.log(insertionSort([4,2,6,1,7,2], (l, r) => l - r));

如果我改用您的比较功能运行它,则它什么也没做:

If I instead run it with your comparison function, it does nothing:

function insertionSort(items, compare) {

  var len = items.length, // number of items in the array
    value, // the value currently being compared
    i, // index into unsorted section
    j; // index into sorted section

  for (i = 0; i < len; i++) {

    // store the current value because it may shift later
    value = items[i];

    for (j = i - 1; j > -1 && compare(value, items[j]) < 0; j--) {
      items[j + 1] = items[j];
    }

    items[j + 1] = value;
  }

  return items;
}

console.log(insertionSort([4,2,6,1,7,2], function(a, b) {
    if (a > b) {
        return 1;
    }
}));

这篇关于为什么compareFunction必须考虑负数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆