简体   繁体   English

如何从字符串的特定部分对列表进行数字排序

[英]How can I sort a list numerically from a specific part of a string

I have this list of links that need to be sorted when they are generated on my page. 我有此链接列表,这些链接在我的页面上生成时需要进行排序。

The links appear like this 链接看起来像这样

"https://www.dropbox.com/s/###/NF%208975%###" "https://www.dropbox.com/s/###/NF%207600%###"

I then map through them when they are generated onto my page, and create a name based on whats at the end of the link (removing all the junk characters). 然后,当它们生成到我的页面上时,我通过它们进行映射,并根据链接末尾的内容创建一个名称(删除所有垃圾字符)。

Is it possible, without altering the url, to sort them numerically based on the what's at the end of the url. 是否可以在不更改url的情况下,根据url末尾的内容对它们进行数字排序。 That way when I map through them, they are already ordered. 这样,当我通过它们映射时,它们已经被订购。

Like this: 像这样:

NF%207600%###

NF%208975%###

I've looked through some similar SO questions, but can't seem to find anything relative enough. 我已经看过一些类似的SO问题,但似乎找不到足够的相关性。

You can .map().sort().map() . 您可以.map().sort().map() It may seem bulky, but if you have a lot of URLs, this is more performant since you don't have to perform a number-parsing regex-replace every time you compare a URL to another one. 它看起来似乎很笨重,但是如果您有很多URL,这会更有效,因为您不必在每次将URL与另一个URL进行比较时都执行数字解析的正则表达式替换。 For example, compare the amount of calls when sorting just 5 URLs: 例如,仅排序5个URL时比较呼叫量:

 var array = [ 'https://www.dropbox.com/s/###/NF%208975%###', 'https://www.dropbox.com/s/###/NF%209213%###', 'https://www.dropbox.com/s/###/NF%205346%###', 'https://www.dropbox.com/s/###/NF%201764%###', 'https://www.dropbox.com/s/###/NF%207600%###' ]; function getId(url) { return Number(url.replace(/^.*%(\\d+)%.*$/, '$1')); } function count(func) { function wrapper() { wrapper.count++; return func.apply(this, arguments); } wrapper.count = 0; return wrapper; } var countGetId = count(getId); var sorted = array .map(url => ({ url, number: countGetId(url) })) .sort((a, b) => a.number - b.number) .map(obj => obj.url); console.log('.map().sort().map()') console.log(sorted); console.log('getId called ' + countGetId.count + ' times'); var countGetId = count(getId); var sorted = array.sort((a, b) => countGetId(a) - countGetId(b)) console.log('.sort()') console.log(sorted); console.log('getId called ' + countGetId.count + ' times'); 

So, just do this: 因此,只需执行以下操作:

function getId(url) {
  return Number(url.replace(/^.*%(\d+)%.*$/, '$1'));
}

var sorted = array
  .map(url => ({ url, number: getId(url) }))
  .sort((a, b) => a.number - b.number)
  .map(obj => obj.url);

console.log(sorted);

You can use regex to extract the number and then compare by it: 您可以使用正则表达式提取数字,然后按此进行比较:

 var urls = ["https://www.dropbox.com/s/###/NF%208975%###", "https://www.dropbox.com/s/###/NF%207600%###"] function extract_number(url) { return Number(url.replace(/.*%(\\d+)%.*/, "$1")) } urls.sort(function(a, b) { if(extract_number(a) < extract_number(b)) { return -1; } if(extract_number(a) > extract_number(b)) { return 1; } return 0; }) console.log(urls); 

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

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