我正在尝试以降序方式对 string[]
进行排序。到目前为止,我所做的是以下代码:
let values = ["Saab", "Volvo", "BMW"]; // example
values.sort();
values.reverse();
它能够工作,但我正在尝试找出是否有更好的方法来实现。
我正在尝试以降序方式对 string[]
进行排序。到目前为止,我所做的是以下代码:
let values = ["Saab", "Volvo", "BMW"]; // example
values.sort();
values.reverse();
它能够工作,但我正在尝试找出是否有更好的方法来实现。
你需要创建一个比较函数,并将其作为sort函数的参数传递:
values.sort((one, two) => (one > two ? -1 : 1));
let values = ["Saab", "Volvo", "BMW"];
values.sort((a, b) => b.localeCompare(a))
由于 localCompare
的输出是一个数字,因此这也不会引起 TypeScript 警告。
更多信息和其他函数参数可以在此处查看。
values.sort((a, b) => b.localeCompare(a, undefined, {numeric: true}))
。 - Davida.toUpperCase() > b.toUpperCase() ? -1 : 1
更优雅的解决方案,但在处理非常大的数据集时要小心,尽管localeCompare
仍然相当快速,但它大约慢了两倍。 - Branislav Popadič使用以下代码对数组进行升序和降序排序。
const ascending: any= values.sort((a,b) => (a > b ? 1 : -1));
const descending: any= values.sort((a,b) => (a > b ? -1 : 1))
这是正常的倒序排列
var nums:number[] = [1,2,3,4]
nums.sort((a,b)=> a < b ? 1:-1)
[ 4, 3, 2, 1 ]
class Student{
name:String
marks:Number
constructor(name:string, marks:number) {
this.name = name
this.marks = marks
}
}
var students:Array<Student> = [
new Student("aseem",47),
new Student("prem",97),
new Student("john",100)
]
console.log(students.sort( (a,b)=> a.marks > b.marks ? -1:1 ))
会导致按照成绩的倒序排序
/usr/local/bin/node /Users/asee2278/git/mycode/JavaScriptLab/typeScript/concepts/sort.js
[
Student { name: 'john', marks: 100 },
Student { name: 'prem', marks: 97 },
Student { name: 'aseem', marks: 47 }
]
我所知道的最新库
https://www.npmjs.com/package/ngx-pipes
const numbers = [2, 1, 3];
const obj = [
{id: 4, name: 'Dave', amount: 2},
{id: 2, name: 'Michael', amount: 2},
{id: 3, name: 'Dan', amount: 1},
{id: 1, name: 'John', amount: 1}
];
const deepObj = [
{id: 1, name: 'John', amount: 1337, deep: {prop: 4}},
{id: 2, name: 'Michael', amount: 42, deep: {prop: 2}},
{id: 3, name: 'Dan', amount: 1, deep: {prop: 1}},
{id: 4, name: 'Dave', amount: 2, deep: {prop: 3}}
];
<!-- Returns array ordered by value -->
<p>{{ numbers | orderBy }}</p> <!-- Output: [1, 2, 3] -->
<p>{{ numbers | orderBy: '-' }}</p> <!-- Output: [3, 2, 1] -->
<!-- Returns array ordered by value of property -->
<p>{{ deepObj | orderBy: 'amount' }}</p>
<!-- Output: [{id: 3, ...}, {id: 4, ...}, {id: 2, ...}, {id: 1, ...}] -->
<p>{{ deepObj | orderBy: '-amount' }}</p>
<!-- Output: [{id: 1, ...}, {id: 2, ...}, {id: 4, ...}, {id: 3, ...}] -->
<!-- Returns array ordered by value of deep property -->
<p>{{ deepObj | orderBy: 'deep.prop' }}</p>
<!-- Output: [{id: 3, ...}, {id: 2, ...}, {id: 4, ...}, {id: 1, ...}] -->
<p>{{ deepObj | orderBy: '-deep.prop' }}</p>
<!-- Output: [{id: 1, ...}, {id: 4, ...}, {id: 2, ...}, {id: 3, ...}] -->
<!-- Returns array ordered by mutliple properties -->
<p>{{ obj | orderBy: ['amount', 'id'] }}</p>
<!-- Output: [{id: 1, ...}, {id: 3, ...}, {id: 2, ...}, {id: 4, ...}] -->
必须以编程方式实现如下
@Component({
// ..
providers: [OrderByPipe]
})
export class AppComponent {
constructor(private orderByPipe: OrderByPipe){
let values = ["Saab", "Volvo", "BMW"];
this.orderByPipe.transform(values, '-');
}
}