简体   繁体   English

Javascript排序索引链接数组

[英]Javascript sort index linked array

Sorry if this has already been asked but I did search "javascript sort index linked array" and found nothing satisfactory. 很抱歉,如果已经有人问我但我确实搜索了“javascript sort index linked array”并且没有发现任何令人满意的结果。

I've got an array of names, and another index linked array which records the frequency at which the names appear in a passage, and I want to sort both arrays not alphabetically but according to the name frequencies - say, most frequent to least frequent. 我有一个名字数组,另一个索引链接数组,记录名称出现在一个段落中的频率,我想按字母顺序排序两个数组,但根据名称频率 - 比如,最频繁到最不频繁。 I've got the following bit of code which does the job adequately, but I'm thinking that it looks like a hack. 我有足够的代码可以完成这项工作,但我认为它看起来像是一个黑客。 Surely there's a more decorous way to solve what must be a pretty common sorting problem. 当然,有一个更高雅的方式来解决必须是一个非常常见的排序问题。

I start with an array of names[] say, 6 Johns, 2 Annes, 9 Toms, 12 Andrews, 3 Kristens, 1 Archie, and 14 Peters - already sorted alphabetically and counted into frequencies, and the routine below results in an array of indexes to the names and frequency arrays which allows me to display the names and frequencies in order from highest to lowest. 我从一系列名字开始[]说,6个约翰,2个安尼斯,9个汤姆斯,12个安德鲁斯,3个克里斯滕斯,1个阿奇和14个彼得斯 - 已按字母顺序排序并计入频率,下面的例程导致一系列名称名称和频率数组的索引,允许我按从高到低的顺序显示名称和频率。

var names = ["Andrew", "Anne", "Archie", "John", "Kristen", "Peter", "Tom"];
var frequency = [12, 2, 1, 6, 3, 14, 9];
var holder = [], secondpart = [], numindex = [];
var i;
for (i = 0; i < frequency.length; i++) {
    if (frequency[i] < 10) {
        holder[i] = "0" + frequency[i] + "!" + i;    // add leading zeros as required
    }
    if (frequency[i] > 9) {
        holder[i] = frequency[i] + "!" + i;    // no leading zeros required
    }
}
holder.sort();
holder.reverse();
for (i = 0; i < holder.length; i++) {
    secondpart[i] = holder[i].substring(holder[i].indexOf("!") + 1, holder[i].length);
    numindex[i] = parseInt(secondpart[i]);
}

I can now list both arrays according to the name frequencies. 我现在可以根据名称频率列出两个数组。

var txt = "";
var useindex;
for (i = 0; i < numindex.length; i++) {
    useindex = numindex[i];
    txt = txt + names[useindex] + " - " + frequency[useindex] + "<br>";
}

Has anyone else had this problem and how did you solve it. 有没有其他人有这个问题,你是如何解决它的。

try this: 尝试这个:

var names = ["Adam", "Peter", "Mahu", "Lala"];
var frequencies = [6,2,9,1];

var tupples=[];
for(let i = 0; i<names.length; i++)
{
    tupples[i] = {
       frequency : frequencies[i],
       name : names[i]
    };
}

//ascending
//tupples.sort(function(a,b){return a.frequency-b.frequency;});

//descending
tupples.sort(function(a,b){return b.frequency-a.frequency;});

for(let i=0; i<tupples.length; i++)
{
    console.debug(tupples[i].name, tupples[i].frequency);
}

Basically you could use the indices and sort them by getting the the frequency for the given index. 基本上你可以使用索引并通过获取给定索引的频率对它们进行排序。

 var names = ['Andrew', 'Anne', 'Archie', 'John', 'Kristen', 'Peter', 'Tom'], frequency = [12, 2, 1, 6, 3, 14, 9], indices = names.map(function(_, i) { return i; }); indices.sort(function(a, b) { return frequency[b] - frequency[a]; }); document.write('<pre>' + JSON.stringify(indices, 0, 4) + '</pre>'); document.write(indices.map(function(a) { return names[a]; }).join('<br>')); 

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

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