array sort
Javascript sort array of objects
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
const paths = [...svgDOM.querySelectorAll('path')];
paths.sort((p1, p2) => {
const bbox1 = p1.getBBox();
const bbox2 = p2.getBBox();
return bbox1.width * bbox1.height > bbox2.width * bbox2.height ? -1 : 1;
}).forEach((path) => {
const pathData = path.getAttribute('d');
const path_points = pathDataToPolys(pathData, {tolerance:1, decimals:1});
log(`path_points`, path_points);
});
sort calback
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
asc / ascending
升序
[...events].sort((a, b) => (a - b > 0) ? 1 : -1);
const arr = [4,5,8,2,3];
arr.sort((a, b) => (a - b > 0) ? 1 : -1);
// [2, 3, 4, 5, 8]
desc / descending
降序
[...events].sort((a, b) => (a - b > 0) ? -1 : 1);
const arr = [4,5,8,2,3];
arr.sort((a, b) => (a - b > 0) ? -1 : 1);
// [8, 5, 4, 3, 2]
https://www.w3schools.com/js/js_array_sort.asp
leetcode
- Kth Largest Element in a Stream
https://leetcode.com/problems/kth-largest-element-in-a-stream/submissions/
class KthLargest {
constructor(k, nums) {
this.k = k;
this.nums = nums;
};
add(val) {
this.nums.push(val);
const arr = this.nums.sort((a, b) => a - b > 0 ? -1 : 1);
return arr[k - 1];
// return [...new Set(arr)][k - 1];
}
}
svg
rect to polygon points
https://codepen.io/xgqfrms/pen/vYOWjYr
refs
©xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!