简体   繁体   English

Javascript:将.sort与compare函数结合使用以查找数组中最长的单词

[英]Javascript: Using .sort with compare function to find longest word in array

I am a beginner student in Javascript. 我是Java语言的初学者。 I have been puzzling over this problem for the last few hours, so I'm hoping that somebody can help me with the the logic in the code I'm trying to write. 在过去的几个小时中,我一直在困惑这个问题,所以我希望有人可以帮助我解决我尝试编写的代码中的逻辑问题。

I am currently trying to write a function that will take the longest string in an array and pass it back to the console. 我目前正在尝试编写一个函数,该函数将采用数组中最长的字符串,并将其传递回控制台。 If there is a tie, the console should return the first word with the most letters. 如果出现平局,则控制台应返回字母数最多的第一个单词。

I found a good piece of code that will do this, but I can't find a way to make it into a function. 我找到了可以做到这一点的好代码,但是我找不到将其变成函数的方法。

arr. sort(function (a, b) { return b.length - a.length })[0];

I tried 我试过了

var longestWord = function(arr) {
    sort(function (a, b) { return b.length - a.length })[0];
}

This doesn't work. 这行不通。 I know I'm doing something wrong, but I'm not sure what it is. 我知道我做错了什么,但是我不确定这是什么。 Also, I can't quite grasp why exactly this words. 另外,我不太清楚为什么要这样说。 I can can understand that the function compares the lengths of the two variables, but why does the 0 index have to be at the end? 我可以理解该函数比较了两个变量的长度,但是为什么0索引必须在末尾?

Any help is greatly appreciated!! 任何帮助是极大的赞赏!!

Your function returns nothing. 您的函数不返回任何内容。 Also, you call sort function, but not Array.prototype.sort . 另外,您可以调用sort函数,而不是Array.prototype.sort

This one does work: 这确实有效:

 var longestWord = function(arr) { return arr.sort(function (a, b) { return b.length - a.length })[0]; } document.body.innerText = longestWord(['a', 'ab', 'abcdef', 'abcd', 'a', 'abcde']); // abcdef 

Answering your "but why does the 0 index have to be at the end" question: Array.prototype.sort returns an array, and you get the first item in an array in order to get the longest word. 回答您的“但为什么0索引必须在末尾”问题: Array.prototype.sort返回一个数组,并且您获得了数组中的第一项以获取最长的单词。 This [0] is an array subscript, ie "first item in an array". [0]是数组下标,即“数组中的第一项”。

These two snippets are equivalent : 这两个片段是等效的

var arr = ['a', 'ab', 'abcdef', 'abcd', 'a', 'abcde']; 
// arr is ['a', 'ab', 'abcdef', 'abcd', 'a', 'abcde']

var arrSorted = arr.sort(function(a, b) { return b.length - a.length; });
// arrSorted is ['abcdef', 'abcde', 'abcd', 'ab', 'a', 'a']

var result = arrSorted[0];
// result is abcdef, the longest word

and

var arr = ['a', 'ab', 'abcdef', 'abcd', 'a', 'abcde']; 
// arr is ['a', 'ab', 'abcdef', 'abcd', 'a', 'abcde']

var result = arr.sort(function(a, b) { return b.length - a.length; })[0];
// result is abcdef, the longest word

The difference is that the second one doesn't store the intermediate result in a variable. 不同之处在于第二个结果不将中间结果存储在变量中。

var my_array = ["a", "aaa", "aa", "aaaaaaa", "a"];
function longestString(arr) {
  return arr.sort(function(a,b) {
    return b.length - a.length;
  })[0];
}
longestString(my_array);

I would suggest use to read this https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort before you use sort(). 我建议您在使用sort()之前先阅读https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/sort Make sure you understand the irator before using it. 使用前请确保您了解该模拟器。

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

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