How to render an Object in a Reverse Sorted/Descending order based upon key in Angular ?
Last Updated :
28 Apr, 2025
An Object is a collection of properties, and a property is an association between a name (or key) and a value. A Property's value can be a function, in which case the property is known as a method. Here, the sorting for an object will be done in descending order based on the key of the object.
In this article, we will see How to render an object in a reverse sorted /descending order in Angular, along with understanding their basic implementation with the help of examples.
Creating Angular application & Module Installation
Step 1: Create an Angular application using the following command.
ng new appname
Step 2: After creating your project folder i.e. appname, move to it using the following command.
cd appname
Project Structure
It will look like the following:

Rendering an object in a reverse sorted /descending order using keyvalue Pair
In this approach, we are using keyvalue Pair, where we have provided objects with keys in order like [1,2,3,4]. Now, we will use keyvalue and sort these keys in reverse as [4,3,2,1]. This can be done by overloading a function called reverseOrder and then implementing it in ts file and returning -1 from there.
Example: This example illustrates the displaying of an object in a reverse sorted /descending order based on key.
HTML
<!-- app.component.html -->
<h2 style="color: green">GeeksforGeeks</h2>
<h2>
How to render an object in a reverse
sorted /descending order based upon
key in Angular
</h2>
<div *ngFor="let item of gfg |keyvalue :reverseOrder">
<b style="color:red">
{{item.key}}
</b>
{{set(item.value)}}
<div *ngFor="let element of gfg2 |keyvalue ">
<ul>
<li>
<b>{{element.key}}</b>
 {{element.value}}
</li>
</ul>
</div>
</div>
JavaScript
// app.component.ts
import { Component, OnInit } from '@angular/core';
import { KeyValue } from '@angular/common';
import { Pipe, PipeTransform } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: "./app.component.html",
styleUrls: ['./app.component.css']
})
export class AppComponent {
gfg: any =
{
"1":
{
"course": "Java",
"cost": "1000"
},
"2":
{
"course": "React",
"cost": "1500"
},
"3":
{
"course": "Angular",
"cost": "1700"
}
,
"4":
{
"course": "CSS",
"cost": "800"
}
}
gfg2: any
set(obj: any) {
this.gfg2 = obj
}
console = console
reverseOrder =
(x: KeyValue<string, any>, y: KeyValue<string, any>): number => {
return -1
}
}
JavaScript
// app.module.ts
import { NgModule }
from '@angular/core';
import { BrowserModule }
from '@angular/platform-browser';
import { HttpClientModule }
from '@angular/common/http';
import { AppComponent }
from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
HttpClientModule,
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Output:

Rendering an object in a reverse sorted /descending order using the sort function
In this approach, we are creating a sort function. This function will sort the values in descending order based on keys. Also, this is done on controller initialization using ngOnInit. Here we had keys in any random order like [3,4,2,1,7,9,8,5]. Output is like [9,8,7,5,4,3,2,1].
Example: This is another example that illustrates the displaying of an object in a reverse sorted /descending order based on key.
HTML
<!-- app.component.html -->
<h2 style="color: green">GeeksforGeeks</h2>
<h2>
How to render an object in a reverse
sorted /descending order based
upon key in Angular
</h2>
<div *ngFor="let item of gfg">
<b>Key: Â </b>{{item.key}} Â
<b>Value : Â </b>{{item.value}}
</div>
JavaScript
// app.component.ts
import { Component, OnInit } from '@angular/core';
import { KeyValue } from '@angular/common';
import { Pipe, PipeTransform } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: "./app.component.html",
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
gfg: any =
[
{ key: "3", value: "Java" },
{ key: "4", value: "React" },
{ key: "2", value: "C++" },
{ key: "1", value: "HTML" },
{ key: "7", value: "Python" },
{ key: "9", value: "CSS" },
{ key: "8", value: "Javascript" },
{ key: "5", value: "Angular" }
]
gfg2: any
console = console
ngOnInit() {
this.gfg.sort(function (a: any, b: any) {
return b.key - a.key;
})
console.log(this.gfg)
}
keepOrder() {
return 0;
}
}
JavaScript
// app.module.ts
import { NgModule }
from '@angular/core';
import { BrowserModule }
from '@angular/platform-browser';
import { HttpClientModule }
from '@angular/common/http';
import { AppComponent }
from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
HttpClientModule,
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Output:

Similar Reads
How to render an Object in a Sorted order based upon Key in Angular ? An Object is a collection of properties, and a property is an association between a name (or key) and a value. A Property's value can be a function, in which case the property is known as a method. To achieve this, we can display the object's properties in a particular order, where the order is dete
3 min read
How to Sort Objects in an Array Based on a Property in a Specific Order in TypeScript ? Sorting objects in an array based on a specific property is a common task in software development. TypeScript, with its static typing and powerful features, provides various approaches to accomplish this task efficiently. The below approaches can be used to sort an array of objects in properties. Ta
3 min read
How to Sort an Array of Objects Based on a Key in JavaScript ? In JavaScript, sorting an array of objects based on the key consists of iterating over the array, applying the sort() method or other approaches, and arranging the array in the desired sorting order. Table of Content Using sort() methodUsing Custom Sorting FunctionUsing Lodash _.orderBy() MethodUsin
3 min read
How to sort collection of MongoDB Database in descending order using Node.js ? MongoDB, the most popular NoSQL database, is an open-source document-oriented database. The term âNoSQLâ means ânon-relationalâ. It means that MongoDB isnât based on the table-like relational database structure but provides an altogether different mechanism for storage and retrieval of data. This fo
2 min read
How to Sort an Array based on User Input in AngularJS ? AngularJS is a JavaScript-based framework. It can be used by adding it to an HTML page using a <script> tag. AngularJS helps in extending the HTML attributes with the help of directives and binding of data to the HTML with expressions. In this article, we will see how to sort an array based o
3 min read