English 中文(简体)
。我将如何在数字列表中根据其中一项指数的真实值排序阵列?
原标题:. How Would I Go About ranking arrays in a numerical list based on the true value of one of its indices
  • 时间:2024-05-19 00:44:39
  •  标签:
  • javascript

我试图构建一个表格, 使我能够通过一个阵列对棒球球员进行排名, 其数字为 Astrributes (1- 100), 通过一个阵列进行排名, 并在设定的索引中按照数字值排列所有所说的阵列。 例如, 如果一个阵列是varar arr = [John James, Start Pitcher, 88] 和 88 是代表速度的 1 - 100 的数, 如何设置一个环形圈, 在按键时, 一个标注速度的列会将50个不同的阵列的ar[ 2] 排排排排排排排排排排排排排, 上面的阵列都有 [ 2] 指数的速分级。 代码下方是我最接近的 。 我是否要在一个阵列内嵌住一个阵列, 以便能够排列这些属性?

>https://www.thesshowrates.com/lists/bunting-bunt 这个链接是一个网站,它通过设定属性,与排名玩家具有相同的特征。 任何帮助都感激不尽。

for (I = 0; I < arr.length; I++){ if (arr[2] == 99.....

问题回答

根据你的描述,我假设你有一个二维阵列, 类似这样的东西:

players = [
  ["Aaron Aaronson", "Position A", 90],
  ["Bob Butcher", "Position B", 80],
  ["Chris Carpenter", "Position C", 70],
];

< a href=> https:// developmenter.mozilla.org/en- US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort" rel=“nofoln noreferr” > default JavaScript 排序行为 是要将每个值(在此情况下,表格的每行)转换成字符串, 并按其 Unicode 代码点进行排序。 在您的情况下, 这将相当于按名称的字母顺序排列玩家。 为了按第三列进行数字排序, 您需要提供一个自定义比较函数。 此函数需要两个值: 如果第一个值较小, 它应该返回负数; 如果第一个值更大, 它应该返回正数; 如果两个值相同, 它应该返回零 。

function compareSpeeds(a, b) {
  return a[2] - b[2]
}

// To sort your array of players:
players.sort(compareSpeeds)

如果您想要用更多的数字列来排序, 您可以定义一个函数, 动态返回指定列的比较函数 :

function cmpCol(col) {
  return (a, b) => a[col] - b[col];
}

// Sort by column 2
players.sort(cmpCol(2));

// Sort by column 3
players.sort(cmpCol(3));

cmpcol 返回的内部函数为rror 函数 。它不必是;您可以这样做:

function cmpCol(col) {
  return function(a, b) {
    return a[col] - b[col]
  }
}

...... 但有好几次 堆叠的回报声明 像这样的 可能会得到有点混乱 和难以阅读。

如果不是 2D 数组, 您的天体数组, 您的解决方案可能看起来是这样的 :

players = [
  {
    name: "Aaron Aaronson",
    role: "Position A",
    speed: 90,
    height: 1.73
  },
  {
    name: "Bob Butcher",
    role: "Position B",
    speed: 80,
    height: 1.78
  },
  {
    name: "Chris Carpenter",
    role: "Position C",
    speed: 70,
    height: 1.75
  }
];

function cmpField(field) {
  return (a, b) => a[field] - b[field];
}

// Sort by speed
players.sort(cmpField("speed"));

// Sort by height
players.sort(cmpField("height"));

请注意, sort 方法会修改原始数组(即它会将它排列在位置上)。如果您宁愿创建原始数组的新的排序副本,您可以使用 tosorted ,否则使用的方法与原数组完全相同:

sortedPlayers = players.toSorted(cmpField("speed"));

如果您还想要能够按非数字字段(如名称)进行排序,这将需要额外的逻辑。这种稍多一点的动词版本将适用于任何支持大于比较的事物,包括字符串和数字:

function cmpField(field) {
  return function(a, b) {
    if (b[field] > a[field]) return -1;
    else if (a[field] > b[field]) return 1;
    else return 0;
  }
}




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签