我在一个包含28个元素的列表中读到:28.6 ft2。列表不按数字顺序读取,一堆用于不同的区域浮点数。然后我点击页面上的过滤器,它将元素从最小到最大排序。我有一个预装了数据的数组,所以当我第一次期望它完全匹配时。我需要采用预先加载的floatarray并将它们从最小到最大排序。 以下是我的代码:
var areaArray = ['5,089.8 ft2', '2,511.5 ft2', '15,076.8 ft2', '5,019.8 ft2', '471.4 ft2', '462.9 ft2', '5,149.3 ft2', '1,753.6 ft2', '6,520.5 ft2', '3,327.4 ft2', '3,192.9 ft2', '2,678.5 ft2',
'2,793.6 ft2', '1,195.0 ft2', '148.5 ft2', '39,077.1 ft2', '5,007.7 ft2', '1,746.4 ft2', '1,247.7 ft2', '230.0 ft2', '345.8 ft2', '114.1 ft2', '229.8 ft2', '349.3 ft2', '116.3 ft2',
'235.2 ft2', '119.5 ft2', '3,038.3 ft2'
];
//the area element list
var area = element.all(by.css("[data-bind='html: area']"));
//this has been shortened per request
}).then(function() {
browser.driver.get('http://iplan-qa.meetingmatrix.com/Apps/CapacityChart/mmidemo/auto/auto');
browser.driver.sleep(2000);
//clicks the area filter button
element.all(by.css("[data-bind='text: displayName, visible: displayName']")).get(3).click().click();
browser.driver.sleep(3000);
area.count().then(function(count) {
console.log(count);
j = 0;
function int_arr(a, b) {
return parseFloat(a) - parseFloat(b);
}
areaArray = areaArray.sort(int_arr);
areaArray = areaArray.reverse();
for (var i = 0; i < count; i++) {
//scrolls down the list element by element
browser.executeScript("arguments[0].scrollIntoView();", area.get(i).getWebElement());
area.get(i).getText().then(function(text) {
console.log(text, areaArray[j], j);
expect(text).toEqual(areaArray[j++]);
});
}
});
});
};
结果如下:
5,089.8 ft2 5,089.8 ft2 0
2,511.5 ft2 2,511.5 ft2 1
15,076.8 ft2 15,076.8 ft2 2
5,019.8 ft2 5,019.8 ft2 3
471.4 ft2 471.4 ft2 4
462.9 ft2 462.9 ft2 5
5,149.3 ft2 5,149.3 ft2 6
1,753.6 ft2 1,753.6 ft2 7
6,520.5 ft2 6,520.5 ft2 8
//this is be shortened per request
39,077.1 ft2 1,247.7 ft2 0
15,076.8 ft2 1,746.4 ft2 1
6,520.5 ft2 1,753.6 ft2 2
5,149.3 ft2 1,195.0 ft2 3
5,089.8 ft2 2,511.5 ft2 4
5,019.8 ft2 2,678.5 ft2 5
5,007.7 ft2 2,793.6 ft2 6
3,327.4 ft2 3,038.3 ft2 7
3,192.9 ft2 3,327.4 ft2 8
答案 0 :(得分:3)
您无法对这些数据进行排序,将整个值解析为数字:
function int_arr(a, b) {
return parseFloat(a) - parseFloat(b); // parseFloat('5,089.8 ft2') -> 5
}
您需要拆分空格上的值并将数字转换为浮点表示,移除,
因此'5,089.8 ft2'
在用于比较时变为5089.8
,而不是5
function int_arr(a, b) {
var na = a.split(' ')[0].replace(',', '');
var nb = b.split(' ')[0].replace(',', '');
return parseFloat(na) - parseFloat(nb);
}
var areaArray = ['5,089.8 ft2', '2,511.5 ft2', '15,076.8 ft2',
'5,019.8 ft2', '471.4 ft2', '462.9 ft2', '5,149.3 ft2', '1,753.6 ft2',
'6,520.5 ft2', '3,327.4 ft2', '3,192.9 ft2', '2,678.5 ft2'
];
areaArray.sort(int_arr);
document.getElementById("result").innerHTML = JSON.stringify(areaArray);
<div id="result"></div>
答案 1 :(得分:1)
我个人只是将现有数组转换为浮点数组,而是使用该浮点数组。这些字符串的逗号和ft2
部分只是显示问题,可以在将HTML写入DOM时添加。
var areaArray = ['5,089.8 ft2', '2,511.5 ft2', '15,076.8 ft2', '5,019.8 ft2', '471.4 ft2', '462.9 ft2', '5,149.3 ft2', '1,753.6 ft2', '6,520.5 ft2', '3,327.4 ft2', '3,192.9 ft2', '2,678.5 ft2',
'2,793.6 ft2', '1,195.0 ft2', '148.5 ft2', '39,077.1 ft2', '5,007.7 ft2', '1,746.4 ft2', '1,247.7 ft2', '230.0 ft2', '345.8 ft2', '114.1 ft2', '229.8 ft2', '349.3 ft2', '116.3 ft2',
'235.2 ft2', '119.5 ft2', '3,038.3 ft2'
];
var areaFloatArray = areaArray.map(function(item) {
return parseFloat(item.replace(' ft2', '').replace(',', ''));
});
// then just work with areaFloatArray, which now contains float values
// you can later add back in commas and "ft2" for display