打字稿 - 对字符串进行降序排序

时间:2016-11-07 18:58:16

标签: arrays typescript

我正在尝试以降序方式对http://<SERVERNAME>/Governance/ChangeDashboard进行排序。到目前为止,我所做的是以下代码:

string[]

它正在运作,但我想弄清楚是否有更好的方法来做到这一点。

5 个答案:

答案 0 :(得分:25)

您需要创建一个比较函数并将其作为sort函数的参数传递:

values.sort((one, two) => (one > two ? -1 : 1));

答案 1 :(得分:7)

一个最新的答案是,您可以利用String.prototype.localCompare()获得数字比较值

简单的例子:

let values = ["Saab", "Volvo", "BMW"];
values.sort((a, b) => b.localeCompare(a))

这也不会引起TypeScript警告,因为localCompare的输出是数字

更多信息和其他功能参数可以在这里https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare

答案 2 :(得分:2)

使用以下代码对数组进行升序和降序排序。

const ascending: any= values.sort((a,b) =>  (a > b ? 1 : -1));
const descending: any= values.sort((a,b) => (a > b ? -1 : 1))

答案 3 :(得分:1)

我知道的最新lib

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, '-');
  }
}

答案 4 :(得分:0)

这是正常的反向排序

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 }
]