【问题标题】:How can I understand this 2 pieces of code?我如何理解这 2 段代码?
【发布时间】:2021-06-09 03:01:54
【问题描述】:
  const [counter, setCounter] = useState(0);
 
  function sortPeople() {
    const sortedPeople = [...peopleList];
    let sortCount = counter;
    if (sortCount === 2) {
      sortCount = 1;
      setCounter(1);
    } else {
      sortCount += 1;
      setCounter(sortCount);
    }
    if (sortCount < 3) {
      sortedPeople.sort(function (x, y) {
        if (sortCount === 1) {
          return x.eaten === y.eaten
            ? 0
            : x.eaten === "No"
            ? -1
            : 1;
        } else if (sortCount === 2) {
          return x.eaten === y.eaten
            ? 0
            : x.eaten === "No"
            ? 1
            : -1;
        }
      });

      setPeopleList(sortedPeople);
    }
  }
  const [counterCount, setCounterCount] = useState(0);
  function sortCountPeople() {
    const sortedCountPeople = [...customerList];
    let sortCountVisit = counterCount;
    if (sortCountVisit === 2) {
      sortCountVisit = 1;
      setCounterCount(1);
    } else {
      sortCountVisit += 1;
      setCounterCount(sortCountVisit);
    }

    sortedCountPeople.sort(function (x, y) {
      if (sortCountVisit === 1) {
        return x.number - y.number;
      } else if (sortCountVisit === 2) {
        return y.number - x.number;
      }
    });
    setPeopleList(sortedCountPeople);
  }

我很难理解:if 排序计数中的排序逻辑是

【问题讨论】:

标签: javascript node.js reactjs frontend


【解决方案1】:

片段 1

if (sortCount < 3) {
  sortedPeople.sort(function (x, y) {
    if (sortCount === 1) {
      return x.eaten === y.eaten
        ? 0
        : x.eaten === "No"
        ? -1
        : 1;
    } else if (sortCount === 2) {
      return x.eaten === y.eaten
        ? 0
        : x.eaten === "No"
        ? 1
        : -1;
    }
  });
}

如果 sortCount 为 0(?)、1 或 2(即小于 3),则 sortedPeople 数组将被排序。

使用比较函数:

function compare(a, b) {
  if (a is less than b by some ordering criterion) {
    return -1;
  }
  if (a is greater than b by the ordering criterion) {
    return 1;
  }
  // a must be equal to b
  return 0;
}

换句话说,如果-1被返回,那么a之前b应该在数组中向前移动,如果1被返回那么aafter b 并且应该移回数组中,如果返回 0 ab 被认为是“相等的”并且 a 保持在原来的位置。

在您的回调中,如果x.eaten 等于y.eaten,则返回0 并且x 不会移动。如果sortCount 等于1 并且x 还没有被吃掉,那么-1 被返回并且x 移回数组中,否则它向前移动。如果sortCount 等于2 则相反,如果x 尚未被吃掉,则返回1 并向前移动,否则向后移动。

更新

由于三元语法似乎存在混淆,我将使用 if-else 语句重写它。

function (x, y) {
  if (sortCount === 1) {
    if (x.eaten === y.eaten) {
      return 0;
    } else if (x.eaten === "No") {
      return -1;
    } else {
      return 1;
    }
  } else if (sortCount === 2) {
    if (x.eaten === y.eaten) {
      return 0;
    } else if (x.eaten === "No") {
      return 1;
    } else {
      return -1;
    }
  }
}

片段 2

sortedCountPeople.sort(function (x, y) {
  if (sortCountVisit === 1) {
    return x.number - y.number;
  } else if (sortCountVisit === 2) {
    return y.number - x.number;
  }
});

比较数字时,比较函数可以简单得多:

function compareNumbers(a, b) {
  return a - b;
}

如果结果为负数或小于零,则a之前 b 并应在数组中向前移动,如果结果为正数或大于零,则a 出现在之后 b 并且应该移回数组中,如果结果是0,它们是相等的,a 保持在原来的位置。

在回调中,如果sortCountVisit 等于1,那么如果x 小于y,则结果将向前移动,如果大于y,则返回。如果 sortCountVisit 等于 2 则相反,x 如果小于 y 则后退,如果大于 y 则前进。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-21
    • 1970-01-01
    • 1970-01-01
    • 2011-04-17
    • 2019-02-25
    • 2011-02-21
    • 1970-01-01
    相关资源
    最近更新 更多