如何在 TypeScript 中对对象数组进行排序?
具体来说,根据一个特定属性对数组对象进行排序,在本例中为
nome
(“姓名”)或 cognome
(“姓氏”)?
/* Object Class*/
export class Test{
nome:String;
cognome:String;
}
/* Generic Component.ts*/
tests:Test[];
test1:Test;
test2:Test;
this.test1.nome='Andrea';
this.test2.nome='Marizo';
this.test1.cognome='Rossi';
this.test2.cognome='Verdi';
this.tests.push(this.test2);
this.tests.push(this.test1);
谢谢!
对我来说最简单的方法是:
升序:
arrayOfObjects.sort((a, b) => (a.propertyToSortBy < b.propertyToSortBy ? -1 : 1));
降序:
arrayOfObjects.sort((a, b) => (a.propertyToSortBy > b.propertyToSortBy ? -1 : 1));
就你而言, 升序:
testsSortedByNome = tests.sort((a, b) => (a.nome < b.nome ? -1 : 1));
testsSortedByCognome = tests.sort((a, b) => (a.cognome < b.cognome ? -1 : 1));
降序:
testsSortedByNome = tests.sort((a, b) => (a.nome > b.nome ? -1 : 1));
testsSortedByCognome = tests.sort((a, b) => (a.cognome > b.cognome ? -1 : 1));
这取决于您要排序的内容。 JavaScript 中的 Array 有标准的 sort 函数,并且可以为对象编写专用的复杂条件。 f.e
var sortedArray: Test[] = unsortedArray.sort((obj1, obj2) => {
if (obj1.cognome > obj2.cognome) {
return 1;
}
if (obj1.cognome < obj2.cognome) {
return -1;
}
return 0;
});
const sorted = unsortedArray.sort((t1, t2) => {
const name1 = t1.name.toLowerCase();
const name2 = t2.name.toLowerCase();
if (name1 > name2) { return 1; }
if (name1 < name2) { return -1; }
return 0;
});
this.tests.sort(t1,t2)=>(t1:Test,t2:Test) => {
if (t1.nome > t2.nome) {
return 1;
}
if (t1.nome < t2.nome) {
return -1;
}
return 0;
}
你尝试过这样的事情吗?
[{nome:'abc'}, {nome:'stu'}, {nome:'cde'}].sort(function(a, b) {
if (a.nome < b.nome)
return -1;
if (a.nome > b.nome)
return 1;
return 0;
});
您可以使用具有泛型类型的函数:
const sortArrayOfObjects = <T>(
data: T[],
keyToSort: keyof T,
direction: 'ascending' | 'descending' | 'none',
) => {
if (direction === 'none') {
return data
}
const compare = (objectA: T, objectB: T) => {
const valueA = objectA[keyToSort]
const valueB = objectB[keyToSort]
if (valueA === valueB) {
return 0
}
if (valueA > valueB) {
return direction === 'ascending' ? 1 : -1
} else {
return direction === 'ascending' ? -1 : 1
}
}
return data.slice().sort(compare)
}
现在,如果您使用该函数对对象数组进行排序,您可以指定要排序的对象属性。
const array = [
{ id: 2, name: "name2" },
{ id: 1, name: "name1" },
{ id: 3, name: "name3" }
];
const sortedArray = sortArrayOfObjects(array, "id", "ascending")
console.log(sortedArray)
//sorted ascending by id
// [
// { id: 1, name: "name1" },
// { id: 2, name: "name2" },
// { id: 3, name: "name3" }
// ]
我经常需要对数据集结果中的单个字段进行排序,比如
Id
,最短的书写方式就是这样
data.sort((a, b) => a.Id - b.Id)
.forEach(row => {
//something with row
});
var arr = [110,5,72,14,12]
升序排序
arr.sort(( a, b ) => a > b ? 1 : -1 )
降序排序
arr.sort(( a, b ) => a < b ? 1 : -1 )
你可以使用这个方法。
let sortedArray: Array<ModelItem>;
sortedArray = unsortedArray.slice(0);
sortedArray.sort((left, right) => {
if (left.id < right.id) return -1;
if (left.id > right.id) return 1;
return 0;
})
我正在使用 Angular 7,我尝试了 Pipe 但没有成功,我尝试了这个并得到了正确的输出。
让对象数组位于对象结果中
results:any ={
"providers": [
{
"name": "AAA",
"error": {
"success": true,
"message": "completed"
},
"quote_id": "BA503452VPC0012790",
"quotes": {
"comprehensive": {
"premiumBreakup": {
"premium": "17398.0",
}
},
}
},
{
"name": "Fi",
"error": {
"success": true,
"message": "completed"
},
"quotes": {
"comprehensive": {
"premiumBreakup": {
"premium": "27957.00"
}
},
}
},
]
}
点击排序功能
<button class="t-sort-optn" (click)="sortByPremium()">Premium</button>
根据保费价值排序
sortByPremium(){
var items = this.results.providers;
console.log("Array",items);
items.sort(function (a, b) {
return a.quotes.comprehensive.premiumBreakup.premium - b.quotes.comprehensive.premiumBreakup.premium;
});
console.log("Array Sorted",items)
}
这对我来说是有效的
.sort((a, b) => {
const a2 = a as unknown as Type;
const b2 = b as unknown as Type;
if (a2.something > b2.something) {
return 1;
}
if (a2.something < b2.something) {
return -1;
}
return 0;
})
将您的数组视为 myArray,
myArray.sort(( a, b ) => a > b ? 1 : -1 )