[英]How to sort an array that contains strings with letters and numbers in JavaScript
I'm working on a web application that make you a test for choosing your professional vocation, it makes 90 questions and depending of your answer it will display a list of the first 3 options of what professions are more appropiate for you. 我正在开发一个Web应用程序,该应用程序可以让您测试选择职业,它会提出90个问题,并且根据您的回答,它将显示最适合您的职业的前三个选项的列表。
The process: 过程:
var vars = [ calc, cFis, CBio]; //array of variables with numeric values of test results.
var keys = object.keys(vars);
var newArray = [];
var names = ['Cálculo', 'Cientifico - Físico', ect]; //array of the names of the professional abilities
//first sorting the array
keys.sort(function(a,b) {
return vars[b] - vars[a];
});
//create newArray whit the results
keys.forEach(function(key) {
newArray.push(names[key] + ' : ' + vars[key]);
});
//print results
document.getElementById('primero').innerHTML = newArray[0];
document.getElementById('segundo').innerHTML = newArray[1];
document.getElementById('tercero').innerHTML = newArray[3];
It will show: 它会显示:
Cálculo : 60
Cientifico - Físico : 45
ect.
The problem 问题
The vars[key]
has to be approximated to a percentage using a table of pre-defined values, like this: 必须使用预定义值表将
vars[key]
近似为一个百分比,如下所示:
Cálculo test points are between 60 - 65 then % must be 80%
Now the string to print must be like : Cálculo : 80%
now, asuming that I have 3 professions and 3 diferents % values, Cálculo : 80%
, C - Fisico : 60%
, art : 30%
I have to display in order of their % value like: 现在要打印的字符串必须像:
Cálculo : 80%
现在为Cálculo : 80%
,假设我有3个职业和3个不同的%值, Cálculo : 80%
, C - Fisico : 60%
, art : 30%
我必须按以下顺序显示其%值如下:
Cálculo : 80% C - Físico : 60% Art : 30%
Is there a way to get the numeric characters on a string for sorting the array in numeric order? 有没有一种方法可以获取字符串中的数字字符,以便按数字顺序对数组进行排序? like the example above?
像上面的例子一样?
I was trying something called natural sort but it always sort the arrays using the alphabetic value before the numeric ones. 我正在尝试一种称为自然排序的方法,但是它总是使用数字值之前的字母值对数组进行排序。 like:
喜欢:
Art : 30% Cálculo : 80% C - Físico 60%
One thing you can do is convert the strings into numbers (removing the %
at the end), then sort the array in descending order: 您可以做的一件事是将字符串转换为数字(在末尾删除
%
),然后以降序对数组进行排序:
var arr = ["Art : 30%", "Cálculo : 80%", "C - Físico: 60%"]; arr.sort(function(a, b){ // Substring between ": <substring here>%". a = +a.substring(a.indexOf(":")+1, a.length - 1); b = +b.substring(b.indexOf(":")+1, b.length - 1); return b - a; // decending order }); console.log(arr);
For items such as "Art : 30%"
you will want the start the substring at the index of ":"+1
. 对于诸如
"Art : 30%"
您需要在索引":"+1
处开始子字符串。
Another way you can do this is to use regular expressions, matching the digits in the strings using .match(/\\d+/g)
to return the string that only contains digits. 执行此操作的另一种方法是使用正则表达式,使用
.match(/\\d+/g)
匹配字符串中的数字,以返回仅包含数字的字符串。
var arr = ["Art : 30%", "Cálculo : 80%", "C - Físico: 60%"]; arr.sort(function(a, b) { a = +a.match(/\\d+/g); b = +b.match(/\\d+/g); return b - a; }); console.log(arr);
Just be careful of using the second method, it won't work if there are numbers being used other than the ones as percents. 请注意使用第二种方法,如果使用的数字不是百分数,则该方法将无效。 For example if one was
"Art1 : 30%"
the second method would not work. 例如,如果一个是
"Art1 : 30%"
则第二种方法将不起作用。 One way to get around that is to use /\\d+%/g
and then remove the %
at the end. 解决该问题的一种方法是使用
/\\d+%/g
,然后最后删除%
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.