简体   繁体   English

按大多数出现的某些字母对数组排序

[英]Sorting an array by most occurrences of certain letters

I have an array of words and the following list of letters: 我有一个单词array和以下字母列表:

searoiltnudcypmhgbkfwvzxjq searoiltnudcypmhgbkfwvzxjq

I would like to sort the array by the letter order in this list, so that the word with the most amount of letters near the start of the list appears first. 我想按此列表中的字母顺序对array进行排序,以便首先出现在列表开头附近的字母数量最多的单词。 For example: the following: 例如:以下内容:

var wordList = "nymph, races, tundra"; 

Once sorted, should be: 排序后,应为:

wordList = "races, tundra, nymph"; 

So far I have the following to assign a weight to each letter in the list: 到目前为止,我可以为列表中的每个字母分配权重:

  for (let h = 0; h < freqWords.length; h++) {
     this["freq" + h] = h; 
  }

Next I have to give each word in the array a value based on how many of these vars it contains and then sort the way from lowest value to highest value. 接下来,我必须根据数组中包含的这些变量的数量为数组中的每个单词赋予一个值,然后从最低值到最高值进行排序。 Unfortunately, I'm not aware of the syntax to accomplish this. 不幸的是,我不知道完成此操作的语法。

A shorter declarative functional-style alternative: 较短的声明性功能样式替代:

const letters = 'searoiltnudcypmhgbkfwvzxjq';
const input = "nymph, races, tundra"
const sorted = input.split(',').map(str => str.trim()).sort((aWord, bWord) => {

    const aCount = aWord.split('').filter(char => letters.includes(char)).length;
    const bCount = bWord.split('').filter(char => letters.includes(char)).length;

    return bCount - aCount;

});

First off, it'll be a lot easier if you convert your letter list into an array and if you convert your word list to an array as well. 首先,如果将字母列表转换为数组,并将单词列表转换为数组,也将容易得多。 Then, you're going to need to write a comparator function for Array.prototype.sort which counts the number of letters a supplied word has in a supplied array of letters. 然后,您将需要为Array.prototype.sort写一个比较器函数,该函数将计算提供的字母数组中提供的单词具有的字母数。 Here's an implementation that solves your problem: 这是解决您的问题的实现:

const letters = [
  's', 'e', 'a', 'r', 'o', 'i', 'l', 't', 'n', 
  'u', 'd', 'c', 'y', 'p', 'm', 'h', 'g', 'b', 
  'k', 'f', 'w', 'v', 'z', 'x', 'j', 'q'
];

const wordList = [
  'nymph', 
  'races', 
  'tundra'  
];

console.log(wordList);

const sortedList = wordList.sort((a, b) => {
  const aOccurences = letterOccurences(letters, a);
  const bOccurences = letterOccurences(letters, b);
  return aOccurences - bOccurences;
});

console.log(sortedList);

function letterOccurences(letters, word) {
  let occurences = 0;
  for (let i = 0; i < word.length; i++) {
    if (letters.includes(word.substring(i, i+1))) {
        occurences++;
    }
  }
  return occurences;
}

Input list: nymph, races, tundra 输入清单: nymph, races, tundra

Result: tundra, nymph, races (6, 5, and 5 occurrences respectively.) 结果: tundra, nymph, races (分别出现tundra, nymph, races和5次。)

EDIT: If you need me to explain anything or if any of the syntax is unfamiliar, let me know. 编辑:如果您需要我解释任何事情,或者任何语法不熟悉,请告诉我。

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

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