In this type script tutorial we can see how to sort an array of an object uing type script.

The array sort() function is used to sort an array. With the specified optional comparison function. Javascript will sort the array object if it is not provided by converting values to strings and comparing strings in order of UTF-16 code units.

syntax:

export class colleges {
name: string;
strength: number;
course: string
}

let college: colleges[] = [];

college.push({
name: “KCDC”,
strength: 300,
course: “Electronics & computer sciences”

Output:

 [{“name”:”KCdC”,”strength”:300,”course”:”Electronics & computer sciences”}]

 

Example : 

export class colleges {

name: string;

strength: number;

course: string

}

let college: colleges[] = [];

company.push({                   

name: “KCDC”,

strength: 250,

course: “Electronics”

});

college.push({

name: “kcdc”,

strength: 300,

course: “bca”

});

college.push({

name: “jjc”,

strength: 150,

course: “intermediate”

});

college.push({

name: “kcjc”,

strength: 423,

course: “mpc”

});

college.push({

name: “jdc”,

revenue: 520,

course: “bsc”

});

college.push({

name: “Rao’s”,

strength: 625.00,

course: “Bcom”

});

Output: 

[{“name”:”jjc”,”strength”:150,”course”:”intermediate”},

{“name”:”KCDC”,”strength”:250,”course”:”Electronics”},
{“name”:”kcdc”,”strength”:300,”course”:”bca”},
{“name”:”kcjc”,”strength”:423,”course”:” mpc”},
{“name”:”jdc”,”strength”:520,”course”:”bsc”},
{“name”:”Rao’s”,”strength”:625,”course”:”Bcom”}]

For any Help or Queries Contact us on info@crmonce.com or +918096556344