简体   繁体   English

如何在排序比较 function 中获取数组元素的索引?

[英]How to get an index of an array element within a sort comparison function?

I am looking at this question:我正在看这个问题:

The sort method for arrays can take an argument that is a comparison function with two parameters—say, x and y. arrays 的sort方法可以接受一个参数,该参数是 function 与两个参数(例如 x 和 y)的比较。 The function returns a negative integer if x should come before y, zero if x and y are indistinguishable, and a positive integer if x should come after y.如果 x 应该在 y 之前,则 function 返回负 integer,如果 x 和 y 无法区分,则返回零,如果 x 应该在 x 之后,则返回正 integer。 Write calls, using arrow functions, that sort:编写调用,使用箭头函数,排序:

  1. An array of positive integers by decreasing order按降序排列的正整数数组
  2. An array of people by increasing age随着年龄的增长,一群人
  3. An array of strings by increasing length增加长度的字符串数组

Here is my code:这是我的代码:

const posIntByDcrOrd = [5,4,3,2,1]
const peopleIncAge = [10,15,20,25,30]
const strIncLength = ['a','ab','abc','abcd']

const compFunc = (x,y) => {
    let sumNeg = y - x
    let sumPos = y + x
    if(indexOf(x) < indexOf(y)) { console.log(sumNeg) }
    else if( indexOf(x) > indexOf(y)) { console.log(sumPos) }
    else { return 0 }
}

posIntByDcrOrd.sort(compFunc(5,4))

The idea behind this code is this: if you can sum the indexes of x and y elements of the arrays, you can get a negative integer, since x will be lower than y and y will be higher than x, which satisfies the conditions.这段代码背后的想法是:如果你可以将 arrays 的 x 和 y 元素的索引相加,你可以得到一个负的 integer,因为 x 将低于 y 并且 y 将高于 x,这满足条件。 But when I try to run this, I got a reference error of course.但是当我尝试运行它时,我当然得到了一个参考错误。 How can I access the index positions of x and y in the sorted array?如何访问已排序数组中 x 和 y 的索引位置? I am open to other solutions as well.我也对其他解决方案持开放态度。

PS: These arrays are made-up for easing the thinking process. PS:这些 arrays 是为了缓解思维过程而编造的。

There is an abundance of Q&A on this site that deal with sorting.这个网站上有大量关于排序的问答。 Your attempt seems to show you haven't seen how numbers are commonly sorted in JavaScript.您的尝试似乎表明您没有看到 JavaScript 中的数字通常是如何排序的。 For instance this Q&A , and many others, provide the right way to do it.例如这个问答和许多其他的,提供了正确的方法来做到这一点。

Some comments on your attempt:对您的尝试的一些评论:

  • There is no indexOf available to you within the callback of sort .sort的回调中没有indexOf可供您使用。 You don't need that information.你不需要这些信息。 Just subtract the second value from the first to get an ascending (non-decreasing) result.只需从第一个值中减去第二个值即可获得递增(非递减)结果。 Perform the subtraction in the other sense to get a descending (non-increasing) result.在另一个意义上执行减法以获得下降(非增加)的结果。 The internals of the sort function will use that return value to perform a sort algorithm. sort function 的内部将使用该返回值来执行排序算法。 No indexes need to be known to you in that process.在该过程中,您不需要知道任何索引。

  • The assignment about people is probably not correctly reflected in your sample array, as now it just looks the same as the first input (an array of numbers).关于人员的分配可能没有正确反映在您的示例数组中,因为现在它看起来与第一个输入(数字数组)相同。 It is quite probable that an array of objects was intended.很可能是一组对象 For example:例如:

     const peopleIncAge = [{name: "Helen", age: 20}, {name: "John", age: 15}, {name: "Anne", age: 30}, {name: "Clark", age: 25}, {name: "Joy", age: 10}]
  • Your input arrays are already sorted as they would need to be output.您的输入 arrays 已经排序,因为它们需要是 output。 For testing any solution, it is better to have them shuffled.为了测试任何解决方案,最好将它们洗牌。

Each of the three exercises needs a different callback function for the sort function:三个练习中的每一个都需要一个不同的回调 function 用于sort function:

 const positives = [1, 3, 5, 4, 2]; const people = [{name: "Helen", age: 20}, {name: "John", age: 15}, {name: "Anne", age: 30}, {name: "Clark", age: 25}, {name: "Joy", age: 10}]; const strings = ['abc', 'a', 'abcd', 'ab']; console.log(positives.sort((x, y) => y - x)); // decreasing console.log(people.sort((x, y) => x.age - y.age)); // increasing age console.log(strings.sort((x, y) => x.length - y.length)); // increasing length

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

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